Skip to content

Commit

Permalink
Add support for Image methods and properties. (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiamurialdo committed Jan 28, 2024
1 parent 6dafc31 commit 6cee276
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dotnet/src/dotnetcore/GxClasses/GxClasses.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,17 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" PrivateAssets="All" />
<PackageReference Include="MySqlConnector" Version="1.0.1" PrivateAssets="All" />
<PackageReference Include="MySqlConnector" Version="1.0.1" PrivateAssets="All" />
<PackageReference Include="NetTopologySuite" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="NetTopologySuite.Core" Version="1.15.3" PrivateAssets="All" />
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="3.3.1" PrivateAssets="All"/>
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="3.3.1" PrivateAssets="All" />
<PackageReference Include="Npgsql" Version="3.2.7" PrivateAssets="All" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.31" PrivateAssets="All" />
<PackageReference Include="Sandwych.GeographicLib" Version="1.49.3" PrivateAssets="All" />
<PackageReference Include="Stubble.Core" Version="1.8.4" />
<PackageReference Include="System.DirectoryServices" Version="4.7.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.5.1" PrivateAssets="All" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.5.1" PrivateAssets="All" />
</ItemGroup>

Expand Down
209 changes: 209 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using Microsoft.Win32;
using System.Security.Cryptography;
using System.Collections.Concurrent;
using System.Drawing.Drawing2D;

namespace GeneXus.Utils
{
Expand Down Expand Up @@ -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 = "/";
Expand Down

0 comments on commit 6cee276

Please sign in to comment.