Skip to content

Commit 6b9ee93

Browse files
committed
Corrected crop size and position when using original size
1 parent c4b6cae commit 6b9ee93

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

src/DatasetCrop/MainWindow.axaml.cs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ await Task.Run(async () =>
181181
});
182182

183183
var loadedImages = await Task.WhenAll(loadTasks);
184-
foreach (var loadedImage in loadedImages)
184+
await Dispatcher.UIThread.InvokeAsync(async () =>
185185
{
186-
await Dispatcher.UIThread.InvokeAsync(async () =>
186+
foreach (var loadedImage in loadedImages)
187187
{
188188
// for each image file, create a grid that contains an image and a drag panel, and add it to the previews list
189189
Grid container = new();
@@ -248,8 +248,8 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
248248
column = 0;
249249
row++;
250250
}
251-
});
252-
}
251+
}
252+
});
253253
});
254254
}
255255

@@ -314,40 +314,44 @@ private async Task CropImagesAsync()
314314
{
315315
var originalImagePath = imageControl.Tag?.ToString()!;
316316
var originalExtension = Path.GetExtension(originalImagePath);
317-
var croppedImagePath = Path.Combine(Path.GetDirectoryName(OutputPath)!, Path.GetFileNameWithoutExtension(originalImagePath) + "-cropped" + originalExtension);
318-
// original image dimensions
319-
var originalWidth = imageControl.Source.Size.Width;
320-
var originalHeight = imageControl.Source.Size.Height;
321-
// calculate scaling factor
322-
var scaleX = originalWidth / imageControl.Bounds.Width;
323-
var scaleY = originalHeight / imageControl.Bounds.Height;
324-
// translate crop area to original scale
325-
double originalCropX, originalCropY, originalCropWidth, originalCropHeight;
326-
if (usesOriginalScaleSizes)
317+
var croppedImagePath = Path.Combine((Path.HasExtension(OutputPath) ? Path.GetDirectoryName(OutputPath) : OutputPath)!, Path.GetFileNameWithoutExtension(originalImagePath) + "-cropped" + originalExtension);
318+
using (var tempImage = await SixLabors.ImageSharp.Image.LoadAsync(originalImagePath))
327319
{
328-
originalCropX = dragPanel.Margin.Left * scaleX;
329-
originalCropY = dragPanel.Margin.Top * scaleY;
330-
originalCropWidth = cropWidth;
331-
originalCropHeight = cropHeight;
320+
// original image dimensions
321+
var originalWidth = tempImage.Width;
322+
var originalHeight = tempImage.Height;
323+
324+
// calculate scaling factor
325+
var scaleX = originalWidth / imageControl.Bounds.Width;
326+
var scaleY = originalHeight / imageControl.Bounds.Height;
327+
// translate crop area to original scale
328+
double originalCropX, originalCropY, originalCropWidth, originalCropHeight;
329+
if (usesOriginalScaleSizes)
330+
{
331+
originalCropX = dragPanel.Margin.Left * scaleX;
332+
originalCropY = dragPanel.Margin.Top * scaleY;
333+
originalCropWidth = cropWidth;
334+
originalCropHeight = cropHeight;
335+
}
336+
else
337+
{
338+
originalCropX = dragPanel.Margin.Left * scaleX;
339+
originalCropY = dragPanel.Margin.Top * scaleY;
340+
originalCropWidth = dragPanel.Width * scaleX;
341+
originalCropHeight = dragPanel.Height * scaleY;
342+
}
343+
byte[] imageBytes = await File.ReadAllBytesAsync(originalImagePath);
344+
IImageFormat imageFormat = originalExtension.ToLower() switch
345+
{
346+
".png" => PngFormat.Instance,
347+
".bmp" => BmpFormat.Instance,
348+
_ => JpegFormat.Instance
349+
};
350+
// perform the crop operation
351+
byte[] croppedImageBytes = await CropImageAsync(tempImage, originalCropX, originalCropY, originalCropWidth, originalCropHeight, imageFormat);
352+
// save the cropped image back to disk
353+
await File.WriteAllBytesAsync(croppedImagePath, croppedImageBytes);
332354
}
333-
else
334-
{
335-
originalCropX = dragPanel.Margin.Left * scaleX;
336-
originalCropY = dragPanel.Margin.Top * scaleY;
337-
originalCropWidth = dragPanel.Width * scaleX;
338-
originalCropHeight = dragPanel.Height * scaleY;
339-
}
340-
byte[] imageBytes = await File.ReadAllBytesAsync(originalImagePath);
341-
IImageFormat imageFormat = originalExtension.ToLower() switch
342-
{
343-
".png" => PngFormat.Instance,
344-
".bmp" => BmpFormat.Instance,
345-
_ => JpegFormat.Instance
346-
};
347-
// perform the crop operation
348-
byte[] croppedImageBytes = await CropImageAsync(imageBytes, originalCropX, originalCropY, originalCropWidth, originalCropHeight, imageFormat);
349-
// save the cropped image back to disk
350-
await File.WriteAllBytesAsync(croppedImagePath, croppedImageBytes);
351355
}
352356
}
353357
await MessageBoxManager.GetMessageBoxStandardWindow("Success!", "Images cropped!", ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Success).ShowDialog(this);
@@ -361,18 +365,17 @@ private async Task CropImagesAsync()
361365
/// <summary>
362366
/// Crops an image to a specified area
363367
/// </summary>
364-
/// <param name="imageBytes">The byte array of the source image</param>
368+
/// <param name="image">The source image</param>
365369
/// <param name="cropX">The X coordinate of the top-left corner of the crop area</param>
366370
/// <param name="cropY">The Y coordinate of the top-left corner of the crop area</param>
367371
/// <param name="cropWidth">The width of the crop area</param>
368372
/// <param name="cropHeight">The height of the crop area</param>
369373
/// <param name="imageFormat">The format of the image to save</param>
370374
/// <returns>The byte array of the cropped image</returns>
371-
private static async Task<byte[]> CropImageAsync(byte[] imageBytes, double cropX, double cropY, double cropWidth, double cropHeight, IImageFormat imageFormat)
375+
private static async Task<byte[]> CropImageAsync(SixLabors.ImageSharp.Image image, double cropX, double cropY, double cropWidth, double cropHeight, IImageFormat imageFormat)
372376
{
373-
using var inputMemoryStream = new MemoryStream(imageBytes);
377+
//using var inputMemoryStream = new MemoryStream(imageBytes);
374378
using var outputMemoryStream = new MemoryStream();
375-
using SixLabors.ImageSharp.Image image = await SixLabors.ImageSharp.Image.LoadAsync(inputMemoryStream);
376379
// define the crop area
377380
var cropRectangle = new Rectangle((int)cropX, (int)cropY, (int)cropWidth, (int)cropHeight);
378381
// crop the image
@@ -611,4 +614,4 @@ private void Window_KeyDown(object? sender, KeyEventArgs e)
611614
isControlPressed = true;
612615
}
613616
#endregion
614-
}
617+
}

0 commit comments

Comments
 (0)