From 6cee276bdeefe094443c1f6b421d9e609ba01a0b Mon Sep 17 00:00:00 2001 From: claudiamurialdo <33756655+claudiamurialdo@users.noreply.github.com> Date: Thu, 25 Feb 2021 12:00:11 -0300 Subject: [PATCH] Add support for Image methods and properties. (#308) https://iwiki.genexus.com/wiki.aspx?24446,Spec%3a+API+para+manipulaci%C3%B3n+de+im%C3%A1genes Co-authored-by: claudia (cherry picked from commit b3e16c21b6b4840a8f72d9907933347ad8d966e4) --- .../src/dotnetcore/GxClasses/GxClasses.csproj | 5 +- .../GxClasses/Core/GXUtilsCommon.cs | 209 ++++++++++++++++++ 2 files changed, 212 insertions(+), 2 deletions(-) diff --git a/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj b/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj index 219efbaa8..02d0107c7 100644 --- a/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj +++ b/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj @@ -150,16 +150,17 @@ - + - + + diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs index d93a060e9..d54624281 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs @@ -36,6 +36,7 @@ using Microsoft.Win32; using System.Security.Cryptography; using System.Collections.Concurrent; +using System.Drawing.Drawing2D; namespace GeneXus.Utils { @@ -5359,6 +5360,214 @@ public static string PathToUrl(string path, bool absUrl, IGxContext context = nu } } + public static class GxImageUtil + { + static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxImageUtil)); + + private static string ImageAbsolutePath(string originalFileLocation) + { + return ImageFile(originalFileLocation).GetAbsoluteName(); + } + private static GxFile ImageFile(string originalFileLocation) + { + return new GxFile(GxContext.StaticPhysicalPath(), originalFileLocation); + } + + public static string Resize(string imageFile, int width, int height, bool keepAspectRatio) + { + try + { + int newheight = height; + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Image image = Image.FromFile(ImageAbsolutePath(originalFileLocation))) + { + // Prevent using images internal thumbnail + image.RotateFlip(RotateFlipType.Rotate180FlipNone); + image.RotateFlip(RotateFlipType.Rotate180FlipNone); + + if (keepAspectRatio) + { + 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.GetThumbnailImage(width, newheight, null, IntPtr.Zero).Save(originalFileLocation); + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"Resize {imageFile} failed", ex); + } + return imageFile; + } + public static string Scale(string imageFile, int percent) + { + try + { + string originalFileLocation = ImageAbsolutePath(imageFile); + int width, height; + using (Image image = Image.FromFile(originalFileLocation)) + { + width = image.Size.Width * percent / 100; + height = image.Size.Height * percent / 100; + } + return Resize(imageFile, width, height, true); + } + catch (Exception ex) + { + GXLogging.Error(log, $"Scale {imageFile} failed", ex); + return imageFile; + } + } + public static string Crop(string imageFile, int X, int Y, int Width, int Height) + { + try + { + using (MemoryStream ms = new MemoryStream()) + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Image OriginalImage = Image.FromFile(originalFileLocation)) + { + using (Bitmap bmp = new Bitmap(Width, Height)) + { + bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution); + using (Graphics Graphic = Graphics.FromImage(bmp)) + { + 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); + bmp.Save(ms, OriginalImage.RawFormat); + } + } + } + using (FileStream file = new FileStream(originalFileLocation, FileMode.Open, FileAccess.Write)) + { + ms.WriteTo(file); + } + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"Crop {imageFile} failed", ex); + } + return imageFile; + } + public static string Rotate(string imageFile, int angle) + { + + try + { + using (MemoryStream ms = new MemoryStream()) + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Image OriginalImage = Image.FromFile(originalFileLocation)) + { + using (Bitmap rotatedImage = new Bitmap(OriginalImage.Width, OriginalImage.Height)) + { + rotatedImage.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution); + + using (Graphics g = Graphics.FromImage(rotatedImage)) + { + g.TranslateTransform(OriginalImage.Width / 2, OriginalImage.Height / 2); + g.RotateTransform(angle); + g.TranslateTransform(-OriginalImage.Width / 2, -OriginalImage.Height / 2); + g.DrawImage(OriginalImage, new Point(0, 0)); + } + rotatedImage.Save(ms, OriginalImage.RawFormat); + } + } + using (FileStream file = new FileStream(originalFileLocation, FileMode.Open, FileAccess.Write)) + { + ms.WriteTo(file); + } + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"Rotate {imageFile} failed", ex); + } + return imageFile; + } + public static string FlipHorizontally(string imageFile) { + + try + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Bitmap bmp = new Bitmap(originalFileLocation)) + { + bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); + bmp.Save(originalFileLocation); + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"Flip Horizontally {imageFile} failed", ex); + } + return imageFile; + } + public static string FlipVertically(string imageFile) + { + try + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Bitmap bmp = new Bitmap(originalFileLocation)) + { + bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); + bmp.Save(originalFileLocation); + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"Flip Vertically {imageFile} failed", ex); + } + return imageFile; + } + + public static int GetImageWidth(string imageFile) + { + try + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Bitmap bmp = new Bitmap(originalFileLocation)) + { + return bmp.Width; + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"GetImageWidth {imageFile} failed", ex); + } + return 0; + } + public static int GetImageHeight(string imageFile) + { + try + { + string originalFileLocation = ImageAbsolutePath(imageFile); + using (Bitmap bmp = new Bitmap(originalFileLocation)) + { + return bmp.Height; + } + } + catch (Exception ex) + { + GXLogging.Error(log, $"GetImageHeight {imageFile} failed", ex); + } + return 0; + } + public static long GetFileSize(string imageFile) + { + try + { + return ImageFile(imageFile).GetLength(); + } + catch (Exception ex) + { + GXLogging.Error(log, $"GetFileSize {imageFile} failed", ex); + } + return 0; + } + } public class StorageUtils { public const string DELIMITER = "/";