mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-24 17:20:58 +12:00
Added aspect ratio support to image watermark when 0 is used for width or height
This commit is contained in:
parent
7df4ad672f
commit
af4060ac88
|
@ -75,18 +75,6 @@ public static Bitmap ResizeImage(Bitmap bmp, Size size, InterpolationMode interp
|
|||
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)
|
||||
{
|
||||
return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage);
|
||||
|
@ -2096,5 +2084,37 @@ public static InterpolationMode GetInterpolationMode(ImageInterpolationMode inte
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using ShareX.HelpersLib;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Design;
|
||||
|
@ -66,35 +67,43 @@ public DrawImage()
|
|||
|
||||
public override Bitmap Apply(Bitmap bmp)
|
||||
{
|
||||
if (SizeMode != DrawImageSizeMode.DontResize && Size.Width <= 0 && Size.Height <= 0)
|
||||
{
|
||||
return bmp;
|
||||
}
|
||||
|
||||
string imageFilePath = Helpers.ExpandFolderVariables(ImageLocation, true);
|
||||
|
||||
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 = bmp2.Size;
|
||||
Size imageSize;
|
||||
|
||||
if (SizeMode == DrawImageSizeMode.AbsoluteSize)
|
||||
{
|
||||
// Use Size property
|
||||
imageSize = Size;
|
||||
imageSize = ImageHelpers.ApplyAspectRatio(Size, bmpWatermark);
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
|
||||
{
|
||||
// Relative size (percentage of watermark)
|
||||
imageSize = new Size((int)(bmp2.Width * (Size.Width / 100.0)), (int)(bmp2.Height * (Size.Height / 100.0)));
|
||||
int width = (int)Math.Round(Size.Width / 100f * bmpWatermark.Width);
|
||||
int height = (int)Math.Round(Size.Height / 100f * bmpWatermark.Height);
|
||||
imageSize = ImageHelpers.ApplyAspectRatio(width, height, bmpWatermark);
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfCanvas)
|
||||
{
|
||||
// Relative size (percentage of image)
|
||||
imageSize = new Size((int)(bmp.Width * (Size.Width / 100.0)), (int)(bmp.Height * (Size.Height / 100.0)));
|
||||
int width = (int)Math.Round(Size.Width / 100f * bmp.Width);
|
||||
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);
|
||||
|
||||
Rectangle imageRectangle = new Rectangle(imagePosition, imageSize);
|
||||
|
||||
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.PixelOffsetMode = PixelOffsetMode.Half;
|
||||
g.CompositingMode = CompositingMode;
|
||||
g.DrawImage(bmp2, imageRectangle);
|
||||
g.DrawImage(bmpWatermark, imageRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,16 +58,15 @@ public override Bitmap Apply(Bitmap bmp)
|
|||
return bmp;
|
||||
}
|
||||
|
||||
int width = Width <= 0 ? (int)((float)Height / bmp.Height * bmp.Width) : Width;
|
||||
int height = Height <= 0 ? (int)((float)Width / bmp.Width * bmp.Height) : Height;
|
||||
Size size = ImageHelpers.ApplyAspectRatio(Width, Height, bmp);
|
||||
|
||||
if ((Mode == ResizeMode.ResizeIfBigger && bmp.Width <= width && bmp.Height <= height) ||
|
||||
(Mode == ResizeMode.ResizeIfSmaller && bmp.Width >= width && bmp.Height >= height))
|
||||
if ((Mode == ResizeMode.ResizeIfBigger && bmp.Width <= size.Width && bmp.Height <= size.Height) ||
|
||||
(Mode == ResizeMode.ResizeIfSmaller && bmp.Width >= size.Width && bmp.Height >= size.Height))
|
||||
{
|
||||
return bmp;
|
||||
}
|
||||
|
||||
return ImageHelpers.ResizeImage(bmp, width, height);
|
||||
return ImageHelpers.ResizeImage(bmp, size);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using ShareX.HelpersLib;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
|
||||
|
@ -49,10 +50,11 @@ public override Bitmap Apply(Bitmap bmp)
|
|||
return bmp;
|
||||
}
|
||||
|
||||
float widthPercentage = WidthPercentage <= 0 ? HeightPercentage : WidthPercentage;
|
||||
float heightPercentage = HeightPercentage <= 0 ? WidthPercentage : HeightPercentage;
|
||||
int width = (int)Math.Round(WidthPercentage / 100 * bmp.Width);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue