Added aspect ratio support to image watermark when 0 is used for width or height

This commit is contained in:
Jaex 2020-06-28 14:05:25 +03:00
parent 7df4ad672f
commit af4060ac88
4 changed files with 63 additions and 33 deletions

View file

@ -75,18 +75,6 @@ public static Bitmap ResizeImage(Bitmap bmp, Size size, InterpolationMode interp
return ResizeImage(bmp, size.Width, size.Height, interpolationMode); return ResizeImage(bmp, size.Width, size.Height, interpolationMode);
} }
public static Bitmap ResizeImageByPercentage(Bitmap bmp, float percentageWidth, float percentageHeight, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
int width = (int)Math.Round(percentageWidth / 100 * bmp.Width);
int height = (int)Math.Round(percentageHeight / 100 * bmp.Height);
return ResizeImage(bmp, width, height, interpolationMode);
}
public static Bitmap ResizeImageByPercentage(Bitmap bmp, float percentage, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
return ResizeImageByPercentage(bmp, percentage, percentage, interpolationMode);
}
public static Bitmap ResizeImage(Bitmap bmp, Size size, bool allowEnlarge, bool centerImage = true) public static Bitmap ResizeImage(Bitmap bmp, Size size, bool allowEnlarge, bool centerImage = true)
{ {
return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage); return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage);
@ -2096,5 +2084,37 @@ public static InterpolationMode GetInterpolationMode(ImageInterpolationMode inte
return InterpolationMode.NearestNeighbor; return InterpolationMode.NearestNeighbor;
} }
} }
public static Size ApplyAspectRatio(int width, int height, Bitmap bmp)
{
int newWidth;
if (width == 0)
{
newWidth = (int)Math.Round((float)height / bmp.Height * bmp.Width);
}
else
{
newWidth = width;
}
int newHeight;
if (height == 0)
{
newHeight = (int)Math.Round((float)width / bmp.Width * bmp.Height);
}
else
{
newHeight = height;
}
return new Size(newWidth, newHeight);
}
public static Size ApplyAspectRatio(Size size, Bitmap bmp)
{
return ApplyAspectRatio(size.Width, size.Height, bmp);
}
} }
} }

View file

@ -24,6 +24,7 @@
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using ShareX.HelpersLib; using ShareX.HelpersLib;
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Drawing.Design; using System.Drawing.Design;
@ -66,35 +67,43 @@ public DrawImage()
public override Bitmap Apply(Bitmap bmp) public override Bitmap Apply(Bitmap bmp)
{ {
if (SizeMode != DrawImageSizeMode.DontResize && Size.Width <= 0 && Size.Height <= 0)
{
return bmp;
}
string imageFilePath = Helpers.ExpandFolderVariables(ImageLocation, true); string imageFilePath = Helpers.ExpandFolderVariables(ImageLocation, true);
if (!string.IsNullOrEmpty(imageFilePath) && File.Exists(imageFilePath)) if (!string.IsNullOrEmpty(imageFilePath) && File.Exists(imageFilePath))
{ {
using (Bitmap bmp2 = ImageHelpers.LoadImage(imageFilePath)) using (Bitmap bmpWatermark = ImageHelpers.LoadImage(imageFilePath))
{ {
if (bmp2 != null) if (bmpWatermark != null)
{ {
// Calculate size first Size imageSize;
Size imageSize = bmp2.Size;
if (SizeMode == DrawImageSizeMode.AbsoluteSize) if (SizeMode == DrawImageSizeMode.AbsoluteSize)
{ {
// Use Size property imageSize = ImageHelpers.ApplyAspectRatio(Size, bmpWatermark);
imageSize = Size;
} }
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark) else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
{ {
// Relative size (percentage of watermark) int width = (int)Math.Round(Size.Width / 100f * bmpWatermark.Width);
imageSize = new Size((int)(bmp2.Width * (Size.Width / 100.0)), (int)(bmp2.Height * (Size.Height / 100.0))); int height = (int)Math.Round(Size.Height / 100f * bmpWatermark.Height);
imageSize = ImageHelpers.ApplyAspectRatio(width, height, bmpWatermark);
} }
else if (SizeMode == DrawImageSizeMode.PercentageOfCanvas) else if (SizeMode == DrawImageSizeMode.PercentageOfCanvas)
{ {
// Relative size (percentage of image) int width = (int)Math.Round(Size.Width / 100f * bmp.Width);
imageSize = new Size((int)(bmp.Width * (Size.Width / 100.0)), (int)(bmp.Height * (Size.Height / 100.0))); int height = (int)Math.Round(Size.Height / 100f * bmp.Height);
imageSize = ImageHelpers.ApplyAspectRatio(width, height, bmp);
}
else
{
imageSize = bmpWatermark.Size;
} }
// Place the image
Point imagePosition = Helpers.GetPosition(Placement, Offset, bmp.Size, imageSize); Point imagePosition = Helpers.GetPosition(Placement, Offset, bmp.Size, imageSize);
Rectangle imageRectangle = new Rectangle(imagePosition, imageSize); Rectangle imageRectangle = new Rectangle(imagePosition, imageSize);
if (AutoHide && !new Rectangle(0, 0, bmp.Width, bmp.Height).Contains(imageRectangle)) if (AutoHide && !new Rectangle(0, 0, bmp.Width, bmp.Height).Contains(imageRectangle))
@ -107,7 +116,7 @@ public override Bitmap Apply(Bitmap bmp)
g.InterpolationMode = ImageHelpers.GetInterpolationMode(InterpolationMode); g.InterpolationMode = ImageHelpers.GetInterpolationMode(InterpolationMode);
g.PixelOffsetMode = PixelOffsetMode.Half; g.PixelOffsetMode = PixelOffsetMode.Half;
g.CompositingMode = CompositingMode; g.CompositingMode = CompositingMode;
g.DrawImage(bmp2, imageRectangle); g.DrawImage(bmpWatermark, imageRectangle);
} }
} }
} }

View file

@ -58,16 +58,15 @@ public override Bitmap Apply(Bitmap bmp)
return bmp; return bmp;
} }
int width = Width <= 0 ? (int)((float)Height / bmp.Height * bmp.Width) : Width; Size size = ImageHelpers.ApplyAspectRatio(Width, Height, bmp);
int height = Height <= 0 ? (int)((float)Width / bmp.Width * bmp.Height) : Height;
if ((Mode == ResizeMode.ResizeIfBigger && bmp.Width <= width && bmp.Height <= height) || if ((Mode == ResizeMode.ResizeIfBigger && bmp.Width <= size.Width && bmp.Height <= size.Height) ||
(Mode == ResizeMode.ResizeIfSmaller && bmp.Width >= width && bmp.Height >= height)) (Mode == ResizeMode.ResizeIfSmaller && bmp.Width >= size.Width && bmp.Height >= size.Height))
{ {
return bmp; return bmp;
} }
return ImageHelpers.ResizeImage(bmp, width, height); return ImageHelpers.ResizeImage(bmp, size);
} }
} }
} }

View file

@ -24,6 +24,7 @@
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using ShareX.HelpersLib; using ShareX.HelpersLib;
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
@ -49,10 +50,11 @@ public override Bitmap Apply(Bitmap bmp)
return bmp; return bmp;
} }
float widthPercentage = WidthPercentage <= 0 ? HeightPercentage : WidthPercentage; int width = (int)Math.Round(WidthPercentage / 100 * bmp.Width);
float heightPercentage = HeightPercentage <= 0 ? WidthPercentage : HeightPercentage; int height = (int)Math.Round(HeightPercentage / 100 * bmp.Height);
Size size = ImageHelpers.ApplyAspectRatio(width, height, bmp);
return ImageHelpers.ResizeImageByPercentage(bmp, widthPercentage, heightPercentage); return ImageHelpers.ResizeImage(bmp, size);
} }
} }
} }