Refactor FillBackground

This commit is contained in:
Jaex 2021-07-18 23:35:18 +03:00
parent e13f893193
commit ed64fb1050
3 changed files with 13 additions and 28 deletions

View file

@ -776,7 +776,6 @@ public static Bitmap FillBackground(Image img, Brush brush)
{ {
Bitmap result = img.CreateEmptyBitmap(); Bitmap result = img.CreateEmptyBitmap();
using (img)
using (Graphics g = Graphics.FromImage(result)) using (Graphics g = Graphics.FromImage(result))
{ {
g.FillRectangle(brush, 0, 0, result.Width, result.Height); g.FillRectangle(brush, 0, 0, result.Width, result.Height);
@ -2478,16 +2477,9 @@ public static void SaveJPEG(Image img, Stream stream, int quality, bool fillBack
{ {
if (fillBackgroundWhite) if (fillBackgroundWhite)
{ {
try using (Image newImage = FillBackground(img, Color.White))
{ {
img = (Image)img.Clone(); SaveJPEGInternal(newImage, stream, quality);
img = FillBackground(img, Color.White);
SaveJPEGInternal(img, stream, quality);
}
finally
{
if (img != null) img.Dispose();
} }
} }
else else

View file

@ -59,12 +59,15 @@ private void AddDefaultGradient()
public override Bitmap Apply(Bitmap bmp) public override Bitmap Apply(Bitmap bmp)
{ {
if (UseGradient && Gradient != null && Gradient.IsValid) using (bmp)
{ {
return ImageHelpers.FillBackground(bmp, Gradient); if (UseGradient && Gradient != null && Gradient.IsValid)
} {
return ImageHelpers.FillBackground(bmp, Gradient);
}
return ImageHelpers.FillBackground(bmp, Color); return ImageHelpers.FillBackground(bmp, Color);
}
} }
} }
} }

View file

@ -299,23 +299,13 @@ public static string CreateThumbnail(Bitmap bmp, string folder, string filename,
if (!string.IsNullOrEmpty(thumbnailFilePath)) if (!string.IsNullOrEmpty(thumbnailFilePath))
{ {
Bitmap thumbnail = null; using (Bitmap thumbnail = (Bitmap)bmp.Clone())
using (Bitmap resizedImage = new Resize(taskSettings.ImageSettings.ThumbnailWidth, taskSettings.ImageSettings.ThumbnailHeight).Apply(thumbnail))
try using (Bitmap newImage = ImageHelpers.FillBackground(resizedImage, Color.White))
{ {
thumbnail = (Bitmap)bmp.Clone(); ImageHelpers.SaveJPEG(newImage, thumbnailFilePath, 90);
thumbnail = new Resize(taskSettings.ImageSettings.ThumbnailWidth, taskSettings.ImageSettings.ThumbnailHeight).Apply(thumbnail);
thumbnail = ImageHelpers.FillBackground(thumbnail, Color.White);
ImageHelpers.SaveJPEG(thumbnail, thumbnailFilePath, 90);
return thumbnailFilePath; return thumbnailFilePath;
} }
finally
{
if (thumbnail != null)
{
thumbnail.Dispose();
}
}
} }
} }