Added "Center" option to "DrawBackgroundImage" image effect

This commit is contained in:
Jaex 2023-05-17 20:05:16 +03:00
parent 43458e9b67
commit c996061d2e
3 changed files with 19 additions and 10 deletions

View file

@ -506,9 +506,9 @@ public static Bitmap AddCanvas(Image img, Padding margin, Color canvasColor)
return bmp;
}
public static Bitmap AddBackgroundImage(Bitmap bmp, Bitmap backgroundImage)
public static Bitmap DrawBackgroundImage(Bitmap bmp, Bitmap backgroundImage, bool center = true)
{
if (backgroundImage != null)
if (bmp != null && backgroundImage != null)
{
using (bmp)
using (backgroundImage)
@ -526,15 +526,21 @@ public static Bitmap AddBackgroundImage(Bitmap bmp, Bitmap backgroundImage)
width = (int)(height * aspectRatio);
}
int centerX = (bmpResult.Width - width) / 2;
int centerY = (bmpResult.Height - height) / 2;
int x = 0;
int y = 0;
if (center)
{
x = (bmpResult.Width - width) / 2;
y = (bmpResult.Height - height) / 2;
}
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
g.DrawImage(backgroundImage, centerX, centerY, width, height);
g.DrawImage(backgroundImage, x, y, width, height);
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
}
@ -545,10 +551,10 @@ public static Bitmap AddBackgroundImage(Bitmap bmp, Bitmap backgroundImage)
return bmp;
}
public static Bitmap AddBackgroundImage(Bitmap bmp, string backgroundImageFilePath)
public static Bitmap DrawBackgroundImage(Bitmap bmp, string backgroundImageFilePath, bool center = true)
{
Bitmap backgroundImage = LoadImage(backgroundImageFilePath);
return AddBackgroundImage(bmp, backgroundImage);
return DrawBackgroundImage(bmp, backgroundImage, center);
}
public static Bitmap RoundedCorners(Bitmap bmp, int cornerRadius)

View file

@ -36,6 +36,9 @@ public class DrawBackgroundImage : ImageEffect
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageFilePath { get; set; }
[DefaultValue(true)]
public bool Center { get; set; }
public DrawBackgroundImage()
{
this.ApplyDefaultPropertyValues();
@ -43,7 +46,7 @@ public DrawBackgroundImage()
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.AddBackgroundImage(bmp, ImageFilePath);
return ImageHelpers.DrawBackgroundImage(bmp, ImageFilePath, Center);
}
}
}

View file

@ -95,11 +95,11 @@ public Bitmap Render(Bitmap image)
}
break;
case ImageBeautifierBackgroundType.Image:
resultImage = ImageHelpers.AddBackgroundImage(resultImage, BackgroundImageFilePath);
resultImage = ImageHelpers.DrawBackgroundImage(resultImage, BackgroundImageFilePath);
break;
case ImageBeautifierBackgroundType.Desktop:
string desktopWallpaperFilePath = Helpers.GetDesktopWallpaperFilePath();
resultImage = ImageHelpers.AddBackgroundImage(resultImage, desktopWallpaperFilePath);
resultImage = ImageHelpers.DrawBackgroundImage(resultImage, desktopWallpaperFilePath);
break;
default:
case ImageBeautifierBackgroundType.Transparent: