From 8bcd86005cc778927ce27dd5a61e7d25fa03ac2a Mon Sep 17 00:00:00 2001 From: cmurialdo Date: Fri, 24 Feb 2023 16:58:58 -0300 Subject: [PATCH] ImageManipulationAPI methods now create a new image instead of modifying the existing one --- .../GxClasses/Core/GXUtilsCommon.cs | 55 +++++++++++++------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs index 985fe93d1..9756fad38 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs @@ -5573,6 +5573,7 @@ private static GxFile ImageFile(string originalFileLocation) public static string Resize(string imageFile, int width, int height, bool keepAspectRatio) { + string modifiedImage = string.Empty; try { using (Image image = ImageCreateFromStream(imageFile)) @@ -5589,15 +5590,15 @@ public static string Resize(string imageFile, int width, int height, bool keepAs double resize = (double)image.Width / (double)width;//get the resize vector newheight = (int)(image.Height / resize);// set the new heigth of the current image }//return the image resized to the given heigth and width - Image output = image.GetThumbnailImage(width, newheight, null, IntPtr.Zero); - Save(output, imageFile, ImageFormat.Bmp); + Image output = image.GetThumbnailImage(width, newheight, null, IntPtr.Zero); + modifiedImage = Save(output, imageFile, ImageFormat.Bmp); } } catch (Exception ex) { GXLogging.Error(log, $"Resize {imageFile} failed", ex); } - return imageFile; + return modifiedImage; } public static string Scale(string imageFile, int percent) { @@ -5620,6 +5621,7 @@ public static string Scale(string imageFile, int percent) } public static string Crop(string imageFile, int X, int Y, int Width, int Height) { + string modifiedImage = string.Empty; try { using (MemoryStream ms = new MemoryStream()) @@ -5634,8 +5636,8 @@ public static string Crop(string imageFile, int X, int Y, int Width, int Height) Graphic.SmoothingMode = SmoothingMode.AntiAlias; Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; - Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel); - Save(bmp, imageFile, OriginalImage.RawFormat); + Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel); + modifiedImage = Save(bmp, imageFile, OriginalImage.RawFormat); } } } @@ -5645,10 +5647,11 @@ public static string Crop(string imageFile, int X, int Y, int Width, int Height) { GXLogging.Error(log, $"Crop {imageFile} failed", ex); } - return imageFile; + return modifiedImage; } public static string Rotate(string imageFile, int angle) { + string modifiedImage = string.Empty; try { using (MemoryStream ms = new MemoryStream()) @@ -5667,7 +5670,7 @@ public static string Rotate(string imageFile, int angle) g.DrawImage(OriginalImage, new Point(0, 0)); } rotatedImage.Save(ms, OriginalImage.RawFormat); - Save(rotatedImage, imageFile, OriginalImage.RawFormat); + modifiedImage = Save(rotatedImage, imageFile, OriginalImage.RawFormat); } } } @@ -5676,60 +5679,76 @@ public static string Rotate(string imageFile, int angle) { GXLogging.Error(log, $"Rotate {imageFile} failed", ex); } - return imageFile; + return modifiedImage; } public static string FlipHorizontally(string imageFile) { + string modifiedImage = string.Empty; try { using (Bitmap bmp = BitmapCreateFromStream(imageFile)) { bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); - return Save(bmp, imageFile, bmp.RawFormat); + modifiedImage = Save(bmp, imageFile, bmp.RawFormat); + return modifiedImage; } } catch (Exception ex) { GXLogging.Error(log, $"Flip Horizontally {imageFile} failed", ex); } - return imageFile; + return modifiedImage; } public static string FlipVertically(string imageFile) { + string modifiedImage = string.Empty; try { using (Bitmap bmp = BitmapCreateFromStream(imageFile)) { bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); - return Save(bmp, imageFile, bmp.RawFormat); + modifiedImage = Save(bmp, imageFile, bmp.RawFormat); + return modifiedImage; } } catch (Exception ex) { GXLogging.Error(log, $"Flip Vertically {imageFile} failed", ex); } - return imageFile; + return modifiedImage; } public static string Save(Image bitmap, string imageFile, ImageFormat format) - { + { + string destinationImagePath = string.Empty; using (MemoryStream ms = new MemoryStream()) { try { bitmap.Save(ms, format); } - catch (Exception) { + catch (Exception) + { //In some cases, copied memory image fails to save when ImageFormat MemoryBmp //https://stackoverflow.com/questions/9073619/image-save-crashing-value-cannot-be-null-r-nparameter-name-encoder bitmap.Save(ms, ImageFormat.Bmp); } ms.Position = 0; - GxFile file = new GxFile(GxContext.StaticPhysicalPath(), imageFile); - file.Create(ms); - file.Close(); + try + { + GxFile sourceImage = new GxFile(GxContext.StaticPhysicalPath(), imageFile); + string destinationImageName = FileUtil.getTempFileName(GxContext.StaticPhysicalPath(), Path.GetFileNameWithoutExtension(sourceImage.GetName()), sourceImage.GetExtension()); + GxFile destinationImage = new GxFile(GxContext.StaticPhysicalPath(), destinationImageName); + destinationImage.Create(ms); + destinationImage.Close(); + destinationImagePath = destinationImage.GetAbsoluteName(); + } + catch (Exception ex) + { + GXLogging.Error(log, $"Save modified image {imageFile} failed", ex); + } } - return imageFile; + return destinationImagePath; } public static int GetImageWidth(string imageFile)