Added "AutoResize" option to "Shadow" image effect

This commit is contained in:
Jaex 2023-04-24 14:34:40 +03:00
parent 1591951c08
commit 96f9cc1493
2 changed files with 26 additions and 7 deletions

View file

@ -1082,7 +1082,7 @@ public static Bitmap AddShadow(Bitmap bmp, float opacity, int size)
return AddShadow(bmp, opacity, size, 1, Color.Black, new Point(0, 0));
}
public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkness, Color color, Point offset)
public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkness, Color color, Point offset, bool autoResize = true)
{
Bitmap bmpShadow = null;
@ -1104,13 +1104,29 @@ public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkne
bmpShadow = shadowImage2;
}
Bitmap bmpResult = bmpShadow.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y));
Bitmap bmpResult;
using (Graphics g = Graphics.FromImage(bmpResult))
if (autoResize)
{
g.SetHighQuality();
g.DrawImage(bmpShadow, Math.Max(0, offset.X), Math.Max(0, offset.Y), bmpShadow.Width, bmpShadow.Height);
g.DrawImage(bmp, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), bmp.Width, bmp.Height);
bmpResult = bmpShadow.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y));
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.DrawImage(bmpShadow, Math.Max(0, offset.X), Math.Max(0, offset.Y), bmpShadow.Width, bmpShadow.Height);
g.DrawImage(bmp, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), bmp.Width, bmp.Height);
}
}
else
{
bmpResult = bmp.CreateEmptyBitmap();
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.DrawImage(bmpShadow, -size + Math.Max(0, offset.X), -size + Math.Max(0, offset.Y), bmpShadow.Width, bmpShadow.Height);
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
}
}
return bmpResult;

View file

@ -71,6 +71,9 @@ public int Size
[DefaultValue(typeof(Point), "0, 0")]
public Point Offset { get; set; }
[DefaultValue(true)]
public bool AutoResize { get; set; }
public Shadow()
{
this.ApplyDefaultPropertyValues();
@ -78,7 +81,7 @@ public Shadow()
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.AddShadow(bmp, Opacity, Size, Darkness + 1, Color, Offset);
return ImageHelpers.AddShadow(bmp, Opacity, Size, Darkness + 1, Color, Offset, AutoResize);
}
}
}