Use bitmap in manipulation image effects

This commit is contained in:
Jaex 2020-03-22 01:07:38 +03:00
parent e419281240
commit c1bcb15d98
19 changed files with 136 additions and 118 deletions

View file

@ -41,18 +41,18 @@ public static class ImageHelpers
{
private const InterpolationMode DefaultInterpolationMode = InterpolationMode.HighQualityBicubic;
public static Image ResizeImage(Image img, int width, int height, InterpolationMode interpolationMode = DefaultInterpolationMode)
public static Bitmap ResizeImage(Bitmap bmp, int width, int height, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
if (width < 1 || height < 1 || (img.Width == width && img.Height == height))
if (width < 1 || height < 1 || (bmp.Width == width && bmp.Height == height))
{
return img;
return bmp;
}
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Bitmap bmpResult = new Bitmap(width, height, PixelFormat.Format32bppArgb);
bmpResult.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
using (img)
using (Graphics g = Graphics.FromImage(bmp))
using (bmp)
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.InterpolationMode = interpolationMode;
g.SmoothingMode = SmoothingMode.HighQuality;
@ -63,28 +63,28 @@ public static Image ResizeImage(Image img, int width, int height, InterpolationM
using (ImageAttributes ia = new ImageAttributes())
{
ia.SetWrapMode(WrapMode.TileFlipXY);
g.DrawImage(img, new Rectangle(0, 0, width, height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
g.DrawImage(bmp, new Rectangle(0, 0, width, height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
}
}
return bmp;
return bmpResult;
}
public static Image ResizeImage(Image img, Size size, InterpolationMode interpolationMode = DefaultInterpolationMode)
public static Bitmap ResizeImage(Bitmap bmp, Size size, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
return ResizeImage(img, size.Width, size.Height, interpolationMode);
return ResizeImage(bmp, size.Width, size.Height, interpolationMode);
}
public static Image ResizeImageByPercentage(Image img, float percentageWidth, float percentageHeight, InterpolationMode interpolationMode = DefaultInterpolationMode)
public static Bitmap ResizeImageByPercentage(Bitmap bmp, float percentageWidth, float percentageHeight, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
int width = (int)Math.Round(percentageWidth / 100 * img.Width);
int height = (int)Math.Round(percentageHeight / 100 * img.Height);
return ResizeImage(img, width, height, interpolationMode);
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 Image ResizeImageByPercentage(Image img, float percentage, InterpolationMode interpolationMode = DefaultInterpolationMode)
public static Bitmap ResizeImageByPercentage(Bitmap bmp, float percentage, InterpolationMode interpolationMode = DefaultInterpolationMode)
{
return ResizeImageByPercentage(img, percentage, percentage, interpolationMode);
return ResizeImageByPercentage(bmp, percentage, percentage, interpolationMode);
}
public static Image ResizeImage(Image img, Size size, bool allowEnlarge, bool centerImage = true)
@ -192,36 +192,36 @@ public static Image CreateThumbnail(Image img, int width, int height)
}
/// <summary>If image size is bigger than specified size then resize it and keep aspect ratio else return image.</summary>
public static Image ResizeImageLimit(Image img, int width, int height)
public static Bitmap ResizeImageLimit(Bitmap bmp, int width, int height)
{
if (img.Width <= width && img.Height <= height)
if (bmp.Width <= width && bmp.Height <= height)
{
return img;
return bmp;
}
double ratioX = (double)width / img.Width;
double ratioY = (double)height / img.Height;
double ratioX = (double)width / bmp.Width;
double ratioY = (double)height / bmp.Height;
if (ratioX < ratioY)
{
height = (int)Math.Round(img.Height * ratioX);
height = (int)Math.Round(bmp.Height * ratioX);
}
else if (ratioX > ratioY)
{
width = (int)Math.Round(img.Width * ratioY);
width = (int)Math.Round(bmp.Width * ratioY);
}
return ResizeImage(img, width, height);
return ResizeImage(bmp, width, height);
}
public static Image ResizeImageLimit(Image img, Size size)
public static Bitmap ResizeImageLimit(Bitmap bmp, Size size)
{
return ResizeImageLimit(img, size.Width, size.Height);
return ResizeImageLimit(bmp, size.Width, size.Height);
}
public static Image ResizeImageLimit(Image img, int size)
public static Bitmap ResizeImageLimit(Bitmap bmp, int size)
{
return ResizeImageLimit(img, size, size);
return ResizeImageLimit(bmp, size, size);
}
public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
@ -445,28 +445,28 @@ public static Bitmap AddCanvas(Image img, Padding margin, Color canvasColor)
return bmp;
}
public static Image RoundedCorners(Image img, int cornerRadius)
public static Bitmap RoundedCorners(Bitmap bmp, int cornerRadius)
{
Bitmap bmp = img.CreateEmptyBitmap();
Bitmap bmpResult = bmp.CreateEmptyBitmap();
using (Graphics g = Graphics.FromImage(bmp))
using (img)
using (bmp)
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.Half;
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddRoundedRectangleProper(new RectangleF(0, 0, img.Width, img.Height), cornerRadius, 0);
gp.AddRoundedRectangleProper(new RectangleF(0, 0, bmp.Width, bmp.Height), cornerRadius, 0);
using (TextureBrush brush = new TextureBrush(img))
using (TextureBrush brush = new TextureBrush(bmp))
{
g.FillPath(brush, gp);
}
}
}
return bmp;
return bmpResult;
}
public static Bitmap Outline(Bitmap bmp, int borderSize, Color borderColor)
@ -527,8 +527,8 @@ public static Bitmap AddSkew(Image img, int x, int y)
{
Bitmap result = img.CreateEmptyBitmap(Math.Abs(x), Math.Abs(y));
using (Graphics g = Graphics.FromImage(result))
using (img)
using (Graphics g = Graphics.FromImage(result))
{
g.SetHighQuality();
int startX = -Math.Min(0, x);
@ -699,8 +699,8 @@ public static Bitmap FillBackground(Image img, Brush brush)
{
Bitmap result = img.CreateEmptyBitmap();
using (Graphics g = Graphics.FromImage(result))
using (img)
using (Graphics g = Graphics.FromImage(result))
{
g.FillRectangle(brush, 0, 0, result.Width, result.Height);
g.DrawImage(img, 0, 0, result.Width, result.Height);
@ -847,20 +847,22 @@ public static void CopyMetadata(Image fromImage, Image toImage)
/// Note that this method always returns a new Bitmap object, even if rotation is zero - in
/// which case the returned object is a clone of the input object.
/// </summary>
/// <param name="img">input Image object, is not modified</param>
/// <param name="bmp">input Image object, is not modified</param>
/// <param name="angleDegrees">angle of rotation, in degrees</param>
/// <param name="upsize">see comments above</param>
/// <param name="clip">see comments above, not used if upsizeOk = true</param>
/// <returns>new Bitmap object, may be larger than input image</returns>
public static Bitmap RotateImage(Image img, float angleDegrees, bool upsize, bool clip)
public static Bitmap RotateImage(Bitmap bmp, float angleDegrees, bool upsize, bool clip)
{
// Test for zero rotation and return a clone of the input image
if (angleDegrees == 0f)
return (Bitmap)img.Clone();
{
return (Bitmap)bmp.Clone();
}
// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK
int oldWidth = img.Width;
int oldHeight = img.Height;
int oldWidth = bmp.Width;
int oldHeight = bmp.Height;
int newWidth = oldWidth;
int newHeight = oldHeight;
float scaleFactor = 1f;
@ -885,10 +887,10 @@ public static Bitmap RotateImage(Image img, float angleDegrees, bool upsize, boo
}
// Create the new bitmap object.
Bitmap newBitmap = img.CreateEmptyBitmap();
Bitmap bmpResult = bmp.CreateEmptyBitmap();
// Create the Graphics object that does the work
using (Graphics g = Graphics.FromImage(newBitmap))
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
@ -898,16 +900,18 @@ public static Bitmap RotateImage(Image img, float angleDegrees, bool upsize, boo
g.TranslateTransform(newWidth / 2f, newHeight / 2f);
if (scaleFactor != 1f)
{
g.ScaleTransform(scaleFactor, scaleFactor);
}
g.RotateTransform(angleDegrees);
g.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
// Draw the result
g.DrawImage(img, 0, 0, img.Width, img.Height);
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
}
return newBitmap;
return bmpResult;
}
public static Bitmap AddShadow(Bitmap bmp, float opacity, int size)
@ -1681,7 +1685,7 @@ public static string SaveImageFileDialog(Image img, string filePath = "", bool u
return null;
}
public static Image LoadImage(string filePath)
public static Bitmap LoadImage(string filePath)
{
try
{
@ -1692,14 +1696,14 @@ public static Image LoadImage(string filePath)
if (!string.IsNullOrEmpty(filePath) && Helpers.IsImageFile(filePath) && File.Exists(filePath))
{
// http://stackoverflow.com/questions/788335/why-does-image-fromfile-keep-a-file-handle-open-sometimes
Image img = Image.FromStream(new MemoryStream(File.ReadAllBytes(filePath)));
Bitmap bmp = (Bitmap)Image.FromStream(new MemoryStream(File.ReadAllBytes(filePath)));
if (HelpersOptions.RotateImageByExifOrientationData)
{
RotateImageByExifOrientationData(img);
RotateImageByExifOrientationData(bmp);
}
return img;
return bmp;
}
}
}
@ -1969,23 +1973,23 @@ public static void DrawColorPickerIcon(Graphics g, Color color, Rectangle rect,
return bmp;
}
public static RotateFlipType RotateImageByExifOrientationData(Image img, bool removeExifOrientationData = true)
public static RotateFlipType RotateImageByExifOrientationData(Bitmap bmp, bool removeExifOrientationData = true)
{
int orientationId = 0x0112;
RotateFlipType rotateType = RotateFlipType.RotateNoneFlipNone;
if (img.PropertyIdList.Contains(orientationId))
if (bmp.PropertyIdList.Contains(orientationId))
{
PropertyItem propertyItem = img.GetPropertyItem(orientationId);
PropertyItem propertyItem = bmp.GetPropertyItem(orientationId);
rotateType = GetRotateFlipTypeByExifOrientationData(propertyItem.Value[0]);
if (rotateType != RotateFlipType.RotateNoneFlipNone)
{
img.RotateFlip(rotateType);
bmp.RotateFlip(rotateType);
if (removeExifOrientationData)
{
img.RemovePropertyItem(orientationId);
bmp.RemovePropertyItem(orientationId);
}
}
}

View file

@ -73,7 +73,7 @@ public static bool ExperimentalCustomTheme
public static Icon Icon => UseWhiteIcon ? Resources.ShareX_Icon_White : Resources.ShareX_Icon;
public static Image Logo => Resources.ShareX_Logo;
public static Bitmap Logo => Resources.ShareX_Logo;
public static ShareXTheme Theme { get; set; } = new ShareXTheme();

View file

@ -301,7 +301,7 @@ private void GeneratePreviewImage(int padding)
if (PreviewImage.Width > 260 && PreviewImage.Height > 260)
{
using (Image logo = ShareXResources.Logo)
using (Bitmap logo = ShareXResources.Logo)
{
g.DrawImage(logo, (PreviewImage.Width / 2) - (logo.Width / 2), (PreviewImage.Height / 2) - (logo.Height / 2));
}

View file

@ -41,9 +41,9 @@ public AutoCrop()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.AutoCropImage((Bitmap)img, false, Sides);
return ImageHelpers.AutoCropImage(bmp, false, Sides);
}
}
}

View file

@ -44,17 +44,17 @@ public Canvas()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
Image result = ImageHelpers.AddCanvas(img, Margin, Color);
Bitmap bmpResult = ImageHelpers.AddCanvas(bmp, Margin, Color);
if (result == null)
if (bmpResult == null)
{
return img;
return bmp;
}
img.Dispose();
return result;
bmp.Dispose();
return bmpResult;
}
}
}

View file

@ -55,11 +55,11 @@ public Crop()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
if (Margin.All == 0) return img;
if (Margin.All == 0) return bmp;
return ImageHelpers.CropBitmap((Bitmap)img, new Rectangle(Margin.Left, Margin.Top, img.Width - Margin.Horizontal, img.Height - Margin.Vertical));
return ImageHelpers.CropBitmap(bmp, new Rectangle(Margin.Left, Margin.Top, bmp.Width - Margin.Horizontal, bmp.Height - Margin.Vertical));
}
}
}

View file

@ -42,7 +42,7 @@ public Flip()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
RotateFlipType flipType = RotateFlipType.RotateNoneFlipNone;
@ -61,10 +61,10 @@ public override Image Apply(Image img)
if (flipType != RotateFlipType.RotateNoneFlipNone)
{
img.RotateFlip(flipType);
bmp.RotateFlip(flipType);
}
return img;
return bmp;
}
}
}

View file

@ -51,20 +51,23 @@ public Resize(int width, int height)
Height = height;
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
if (Width <= 0 && Height <= 0) return img;
int width = Width <= 0 ? (int)((float)Height / img.Height * img.Width) : Width;
int height = Height <= 0 ? (int)((float)Width / img.Width * img.Height) : Height;
if ((Mode == ResizeMode.ResizeIfBigger && img.Width <= width && img.Height <= height) ||
(Mode == ResizeMode.ResizeIfSmaller && img.Width >= width && img.Height >= height))
if (Width <= 0 && Height <= 0)
{
return img;
return bmp;
}
return ImageHelpers.ResizeImage(img, width, height);
int width = Width <= 0 ? (int)((float)Height / bmp.Height * bmp.Width) : Width;
int height = Height <= 0 ? (int)((float)Width / bmp.Width * bmp.Height) : Height;
if ((Mode == ResizeMode.ResizeIfBigger && bmp.Width <= width && bmp.Height <= height) ||
(Mode == ResizeMode.ResizeIfSmaller && bmp.Width >= width && bmp.Height >= height))
{
return bmp;
}
return ImageHelpers.ResizeImage(bmp, width, height);
}
}
}

View file

@ -45,9 +45,17 @@ public Rotate()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.RotateImage(img, Angle, Upsize, Clip);
if (Angle == 0)
{
return bmp;
}
using (bmp)
{
return ImageHelpers.RotateImage(bmp, Angle, Upsize, Clip);
}
}
}
}

View file

@ -52,9 +52,9 @@ public RoundedCorners()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.RoundedCorners(img, CornerRadius);
return ImageHelpers.RoundedCorners(bmp, CornerRadius);
}
}
}

View file

@ -42,14 +42,17 @@ public Scale()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
if (WidthPercentage <= 0 && HeightPercentage <= 0) return img;
if (WidthPercentage <= 0 && HeightPercentage <= 0)
{
return bmp;
}
float widthPercentage = WidthPercentage <= 0 ? HeightPercentage : WidthPercentage;
float heightPercentage = HeightPercentage <= 0 ? WidthPercentage : HeightPercentage;
return ImageHelpers.ResizeImageByPercentage(img, widthPercentage, heightPercentage);
return ImageHelpers.ResizeImageByPercentage(bmp, widthPercentage, heightPercentage);
}
}
}

View file

@ -42,14 +42,14 @@ public Skew()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
if (Horizontally == 0 && Vertically == 0)
{
return img;
return bmp;
}
return ImageHelpers.AddSkew(img, Horizontally, Vertically);
return ImageHelpers.AddSkew(bmp, Horizontally, Vertically);
}
}
}

View file

@ -35,7 +35,7 @@ public class WatermarkConfig
public DrawText Text = new DrawText { DrawTextShadow = false };
public DrawImage Image = new DrawImage();
public Image Apply(Image img)
public Bitmap Apply(Bitmap bmp)
{
Text.Placement = Image.Placement = Placement;
Text.Offset = Image.Offset = new Point(Offset, Offset);
@ -44,9 +44,9 @@ public Image Apply(Image img)
{
default:
case WatermarkType.Text:
return Text.Apply(img);
return Text.Apply(bmp);
case WatermarkType.Image:
return Image.Apply(img);
return Image.Apply(bmp);
}
}
}

View file

@ -192,7 +192,7 @@ private int GetRandomTimeSlice(int start)
private Image CombineScreenshots(List<VideoThumbnailInfo> thumbnails)
{
List<Image> images = new List<Image>();
List<Bitmap> images = new List<Bitmap>();
Image finalImage = null;
try
@ -212,15 +212,15 @@ private Image CombineScreenshots(List<VideoThumbnailInfo> thumbnails)
foreach (VideoThumbnailInfo thumbnail in thumbnails)
{
Image img = ImageHelpers.LoadImage(thumbnail.Filepath);
Bitmap bmp = ImageHelpers.LoadImage(thumbnail.Filepath);
if (Options.MaxThumbnailWidth > 0 && img.Width > Options.MaxThumbnailWidth)
if (Options.MaxThumbnailWidth > 0 && bmp.Width > Options.MaxThumbnailWidth)
{
int maxThumbnailHeight = (int)((float)Options.MaxThumbnailWidth / img.Width * img.Height);
img = ImageHelpers.ResizeImage(img, Options.MaxThumbnailWidth, maxThumbnailHeight);
int maxThumbnailHeight = (int)((float)Options.MaxThumbnailWidth / bmp.Width * bmp.Height);
bmp = ImageHelpers.ResizeImage(bmp, Options.MaxThumbnailWidth, maxThumbnailHeight);
}
images.Add(img);
images.Add(bmp);
}
int columnCount = Options.ColumnCount;
@ -317,7 +317,7 @@ private Image CombineScreenshots(List<VideoThumbnailInfo> thumbnails)
}
finally
{
foreach (Image image in images)
foreach (Bitmap image in images)
{
if (image != null)
{

View file

@ -105,15 +105,15 @@ private bool LoadSticker(string filePath, int stickerSize)
{
if (!string.IsNullOrEmpty(filePath))
{
Image img = ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (img != null)
if (bmp != null)
{
AnnotationOptions.LastStickerPath = filePath;
img = ImageHelpers.ResizeImageLimit(img, stickerSize);
bmp = ImageHelpers.ResizeImageLimit(bmp, stickerSize);
SetImage(img, true);
SetImage(bmp, true);
return true;
}

View file

@ -347,7 +347,7 @@ private void InitializeComponent()
public class NotificationFormConfig : IDisposable
{
public Image Image { get; set; }
public Bitmap Image { get; set; }
public string Text { get; set; }
public string FilePath { get; set; }
public string URL { get; set; }

View file

@ -256,31 +256,31 @@ public static ImageData PrepareImage(Image img, TaskSettings taskSettings)
return imageData;
}
public static string CreateThumbnail(Image img, string folder, string filename, TaskSettings taskSettings)
public static string CreateThumbnail(Bitmap bmp, string folder, string filename, TaskSettings taskSettings)
{
if ((taskSettings.ImageSettings.ThumbnailWidth > 0 || taskSettings.ImageSettings.ThumbnailHeight > 0) && (!taskSettings.ImageSettings.ThumbnailCheckSize ||
(img.Width > taskSettings.ImageSettings.ThumbnailWidth && img.Height > taskSettings.ImageSettings.ThumbnailHeight)))
(bmp.Width > taskSettings.ImageSettings.ThumbnailWidth && bmp.Height > taskSettings.ImageSettings.ThumbnailHeight)))
{
string thumbnailFileName = Path.GetFileNameWithoutExtension(filename) + taskSettings.ImageSettings.ThumbnailName + ".jpg";
string thumbnailFilePath = HandleExistsFile(folder, thumbnailFileName, taskSettings);
if (!string.IsNullOrEmpty(thumbnailFilePath))
{
Image thumbImage = null;
Bitmap thumbnail = null;
try
{
thumbImage = (Image)img.Clone();
thumbImage = new Resize(taskSettings.ImageSettings.ThumbnailWidth, taskSettings.ImageSettings.ThumbnailHeight).Apply(thumbImage);
thumbImage = ImageHelpers.FillBackground(thumbImage, Color.White);
thumbImage.SaveJPG(thumbnailFilePath, 90);
thumbnail = (Bitmap)bmp.Clone();
thumbnail = new Resize(taskSettings.ImageSettings.ThumbnailWidth, taskSettings.ImageSettings.ThumbnailHeight).Apply(thumbnail);
thumbnail = ImageHelpers.FillBackground(thumbnail, Color.White);
thumbnail.SaveJPG(thumbnailFilePath, 90);
return thumbnailFilePath;
}
finally
{
if (thumbImage != null)
if (thumbnail != null)
{
thumbImage.Dispose();
thumbnail.Dispose();
}
}
}

View file

@ -390,7 +390,7 @@ private static void Task_TaskCompleted(WorkerTask task)
RightClickAction = info.TaskSettings.AdvancedSettings.ToastWindowRightClickAction,
MiddleClickAction = info.TaskSettings.AdvancedSettings.ToastWindowMiddleClickAction,
FilePath = info.FilePath,
Image = task.Image,
Image = (Bitmap)task.Image,
Text = "ShareX - " + Resources.TaskManager_task_UploadCompleted_ShareX___Task_completed + "\r\n" + result,
URL = result
};

View file

@ -683,7 +683,7 @@ private bool DoAfterCaptureJobs()
thumbnailFolder = Info.TaskSettings.CaptureFolder;
}
Info.ThumbnailFilePath = TaskHelpers.CreateThumbnail(Image, thumbnailFolder, thumbnailFilename, Info.TaskSettings);
Info.ThumbnailFilePath = TaskHelpers.CreateThumbnail((Bitmap)Image, thumbnailFolder, thumbnailFilename, Info.TaskSettings);
if (!string.IsNullOrEmpty(Info.ThumbnailFilePath))
{