All ImageHelpers methods return bitmap now instead of image

This commit is contained in:
Jaex 2020-03-22 02:22:43 +03:00
parent c1bcb15d98
commit 0137f60a0d
19 changed files with 111 additions and 111 deletions

View file

@ -56,11 +56,11 @@ public static void ShowImage(Image img)
public static void ShowImage(string filePath)
{
using (Image img = ImageHelpers.LoadImage(filePath))
using (Bitmap bmp = ImageHelpers.LoadImage(filePath))
{
if (img != null)
if (bmp != null)
{
using (ImageViewer viewer = new ImageViewer(img))
using (ImageViewer viewer = new ImageViewer(bmp))
{
viewer.ShowDialog();
}

View file

@ -68,9 +68,9 @@ public void AddFrame(Image img, GIFQuality quality = GIFQuality.Default)
public void AddFrame(string path, GIFQuality quality = GIFQuality.Default)
{
using (Image img = ImageHelpers.LoadImage(path))
using (Bitmap bmp = ImageHelpers.LoadImage(path))
{
AddFrame(img, quality);
AddFrame(bmp, quality);
}
}

View file

@ -203,9 +203,9 @@ public static bool CopyImageFromFile(string path)
{
try
{
using (Image img = ImageHelpers.LoadImage(path))
using (Bitmap bmp = ImageHelpers.LoadImage(path))
{
return CopyImage(img);
return CopyImage(bmp);
}
}
catch (Exception e)

View file

@ -87,34 +87,34 @@ public static Bitmap ResizeImageByPercentage(Bitmap bmp, float percentage, Inter
return ResizeImageByPercentage(bmp, percentage, percentage, interpolationMode);
}
public static Image ResizeImage(Image img, Size size, bool allowEnlarge, bool centerImage = true)
public static Bitmap ResizeImage(Bitmap bmp, Size size, bool allowEnlarge, bool centerImage = true)
{
return ResizeImage(img, size.Width, size.Height, allowEnlarge, centerImage);
return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage);
}
public static Image ResizeImage(Image img, int width, int height, bool allowEnlarge, bool centerImage = true)
public static Bitmap ResizeImage(Bitmap bmp, int width, int height, bool allowEnlarge, bool centerImage = true)
{
return ResizeImage(img, width, height, allowEnlarge, centerImage, Color.Transparent);
return ResizeImage(bmp, width, height, allowEnlarge, centerImage, Color.Transparent);
}
public static Image ResizeImage(Image img, int width, int height, bool allowEnlarge, bool centerImage, Color backColor)
public static Bitmap ResizeImage(Bitmap bmp, int width, int height, bool allowEnlarge, bool centerImage, Color backColor)
{
double ratio;
int newWidth, newHeight;
if (!allowEnlarge && img.Width <= width && img.Height <= height)
if (!allowEnlarge && bmp.Width <= width && bmp.Height <= height)
{
ratio = 1.0;
newWidth = img.Width;
newHeight = img.Height;
newWidth = bmp.Width;
newHeight = bmp.Height;
}
else
{
double ratioX = (double)width / img.Width;
double ratioY = (double)height / img.Height;
double ratioX = (double)width / bmp.Width;
double ratioY = (double)height / bmp.Height;
ratio = ratioX < ratioY ? ratioX : ratioY;
newWidth = (int)(img.Width * ratio);
newHeight = (int)(img.Height * ratio);
newWidth = (int)(bmp.Width * ratio);
newHeight = (int)(bmp.Height * ratio);
}
int newX = 0;
@ -122,14 +122,14 @@ public static Image ResizeImage(Image img, int width, int height, bool allowEnla
if (centerImage)
{
newX += (int)((width - (img.Width * ratio)) / 2);
newY += (int)((height - (img.Height * ratio)) / 2);
newX += (int)((width - (bmp.Width * ratio)) / 2);
newY += (int)((height - (bmp.Height * ratio)) / 2);
}
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 (Graphics g = Graphics.FromImage(bmp))
using (Graphics g = Graphics.FromImage(bmpResult))
{
if (backColor.A > 0)
{
@ -137,15 +137,15 @@ public static Image ResizeImage(Image img, int width, int height, bool allowEnla
}
g.SetHighQuality();
g.DrawImage(img, newX, newY, newWidth, newHeight);
g.DrawImage(bmp, newX, newY, newWidth, newHeight);
}
return bmp;
return bmpResult;
}
public static Image CreateThumbnail(Image img, int width, int height)
public static Bitmap CreateThumbnail(Bitmap bmp, int width, int height)
{
double srcRatio = (double)img.Width / img.Height;
double srcRatio = (double)bmp.Width / bmp.Height;
double dstRatio = (double)width / height;
int w, h;
@ -153,42 +153,42 @@ public static Image CreateThumbnail(Image img, int width, int height)
{
if (srcRatio >= 1)
{
w = (int)(img.Height * dstRatio);
w = (int)(bmp.Height * dstRatio);
}
else
{
w = (int)(img.Width / srcRatio * dstRatio);
w = (int)(bmp.Width / srcRatio * dstRatio);
}
h = img.Height;
h = bmp.Height;
}
else
{
w = img.Width;
w = bmp.Width;
if (srcRatio >= 1)
{
h = (int)(img.Height / dstRatio * srcRatio);
h = (int)(bmp.Height / dstRatio * srcRatio);
}
else
{
h = (int)(img.Height * srcRatio / dstRatio);
h = (int)(bmp.Height * srcRatio / dstRatio);
}
}
int x = (img.Width - w) / 2;
int y = (img.Height - h) / 2;
int x = (bmp.Width - w) / 2;
int y = (bmp.Height - h) / 2;
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 (Graphics g = Graphics.FromImage(bmp))
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
g.DrawImage(bmp, new Rectangle(0, 0, width, height), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
}
return bmp;
return bmpResult;
}
/// <summary>If image size is bigger than specified size then resize it and keep aspect ratio else return image.</summary>
@ -750,17 +750,17 @@ public static Bitmap DrawCheckers(int width, int height, int checkerSize, Color
return bmp;
}
public static Image CreateCheckerPattern()
public static Bitmap CreateCheckerPattern()
{
return CreateCheckerPattern(10, 10);
}
public static Image CreateCheckerPattern(int width, int height)
public static Bitmap CreateCheckerPattern(int width, int height)
{
return CreateCheckerPattern(width, height, SystemColors.ControlLight, SystemColors.ControlLightLight);
}
public static Image CreateCheckerPattern(int width, int height, Color checkerColor1, Color checkerColor2)
public static Bitmap CreateCheckerPattern(int width, int height, Color checkerColor1, Color checkerColor2)
{
Bitmap bmp = new Bitmap(width * 2, height * 2);
@ -1715,7 +1715,7 @@ public static Bitmap LoadImage(string filePath)
return null;
}
public static Image LoadImageWithFileDialog()
public static Bitmap LoadImageWithFileDialog()
{
string filepath = OpenImageFileDialog();
@ -1727,7 +1727,7 @@ public static Image LoadImageWithFileDialog()
return null;
}
public static Image CombineImages(IEnumerable<Image> images, Orientation orientation = Orientation.Vertical, int space = 0)
public static Bitmap CombineImages(IEnumerable<Image> images, Orientation orientation = Orientation.Vertical, int space = 0)
{
int width, height;
@ -1773,9 +1773,9 @@ public static Image CombineImages(IEnumerable<Image> images, Orientation orienta
return bmp;
}
public static List<Image> SplitImage(Image img, int rowCount, int columnCount)
public static List<Bitmap> SplitImage(Image img, int rowCount, int columnCount)
{
List<Image> images = new List<Image>();
List<Bitmap> images = new List<Bitmap>();
int width = img.Width / columnCount;
int height = img.Height / rowCount;
@ -1800,7 +1800,7 @@ public static List<Image> SplitImage(Image img, int rowCount, int columnCount)
return images;
}
public static Image CreateColorPickerIcon(Color color, Rectangle rect, int holeSize = 0)
public static Bitmap CreateColorPickerIcon(Color color, Rectangle rect, int holeSize = 0)
{
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
@ -2039,11 +2039,11 @@ public static void SelectiveColor(Bitmap bmp, Color lightColor, Color darkColor,
public static Size GetImageFileDimensions(string filePath)
{
using (Image img = LoadImage(filePath))
using (Bitmap bmp = LoadImage(filePath))
{
if (img != null)
if (bmp != null)
{
return img.Size;
return bmp.Size;
}
}

View file

@ -31,23 +31,23 @@ namespace ShareX.HelpersLib
{
public class ImageFilesCache : IDisposable
{
private Dictionary<string, Image> images = new Dictionary<string, Image>();
private Dictionary<string, Bitmap> images = new Dictionary<string, Bitmap>();
public Image GetImage(string filePath)
public Bitmap GetImage(string filePath)
{
if (images.ContainsKey(filePath))
{
return images[filePath];
}
Image img = ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (img != null)
if (bmp != null)
{
images.Add(filePath, img);
images.Add(filePath, bmp);
}
return img;
return bmp;
}
public void Clear()
@ -64,11 +64,11 @@ public void Dispose()
{
if (images != null)
{
foreach (Image img in images.Values)
foreach (Bitmap bmp in images.Values)
{
if (img != null)
if (bmp != null)
{
img.Dispose();
bmp.Dispose();
}
}
}

View file

@ -61,12 +61,12 @@ public override Bitmap Apply(Bitmap bmp)
{
if (!string.IsNullOrEmpty(ImageLocation) && File.Exists(ImageLocation))
{
using (Image img = ImageHelpers.LoadImage(ImageLocation))
using (Bitmap bmp2 = ImageHelpers.LoadImage(ImageLocation))
{
if (img != null)
if (bmp2 != null)
{
// Calculate size first
Size imageSize = img.Size;
Size imageSize = bmp2.Size;
if (SizeMode == DrawImageSizeMode.AbsoluteSize)
{
// Use Size property
@ -75,7 +75,7 @@ public override Bitmap Apply(Bitmap bmp)
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
{
// Relative size (percentage of watermark)
imageSize = new Size((int)(img.Width * (Size.Width / 100.0)), (int)(img.Height * (Size.Height / 100.0)));
imageSize = new Size((int)(bmp2.Width * (Size.Width / 100.0)), (int)(bmp2.Height * (Size.Height / 100.0)));
}
else if (SizeMode == DrawImageSizeMode.PercentageOfCanvas)
{
@ -96,7 +96,7 @@ public override Bitmap Apply(Bitmap bmp)
using (Graphics g = Graphics.FromImage(bmp))
{
g.SetHighQuality();
g.DrawImage(img, imageRectangle);
g.DrawImage(bmp2, imageRectangle);
}
}
}

View file

@ -118,11 +118,11 @@ public override Bitmap Apply(Bitmap bmp)
{
string file = MathHelpers.RandomPick(files);
Image img = imageCache.GetImage(file);
Bitmap bmpCached = imageCache.GetImage(file);
if (img != null)
if (bmpCached != null)
{
DrawImage(bmp, img, g);
DrawImage(bmp, bmpCached, g);
}
}
}

View file

@ -47,7 +47,7 @@ public Bitmap ApplyEffects(Bitmap bmp)
{
foreach (ImageEffect effect in Effects.Where(x => x.Enabled))
{
result = (Bitmap)effect.Apply(result);
result = effect.Apply(result);
if (result == null)
{

View file

@ -606,7 +606,7 @@ private void tsmiLoadImageFromFile_Click(object sender, EventArgs e)
if (!string.IsNullOrEmpty(filePath))
{
if (PreviewImage != null) PreviewImage.Dispose();
PreviewImage = (Bitmap)ImageHelpers.LoadImage(filePath);
PreviewImage = ImageHelpers.LoadImage(filePath);
FilePath = filePath;
UpdatePreview();
}
@ -680,7 +680,7 @@ private void pbResult_DragDrop(object sender, DragEventArgs e)
if (Helpers.IsImageFile(files[0]))
{
if (PreviewImage != null) PreviewImage.Dispose();
PreviewImage = (Bitmap)ImageHelpers.LoadImage(files[0]);
PreviewImage = ImageHelpers.LoadImage(files[0]);
UpdatePreview();
}
}

View file

@ -115,7 +115,7 @@ private void btnCombine_Click(object sender, EventArgs e)
{
if (lvImages.Items.Count > 0)
{
List<Image> images = new List<Image>();
List<Bitmap> images = new List<Bitmap>();
try
{
@ -125,11 +125,11 @@ private void btnCombine_Click(object sender, EventArgs e)
if (File.Exists(filePath))
{
Image img = ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (img != null)
if (bmp != null)
{
images.Add(img);
images.Add(bmp);
}
}
}
@ -150,7 +150,7 @@ private void btnCombine_Click(object sender, EventArgs e)
{
if (images != null)
{
foreach (Image image in images)
foreach (Bitmap image in images)
{
if (image != null)
{

View file

@ -57,11 +57,11 @@ private List<string> SplitImage(string filePath, int rowCount, int columnCount,
{
List<string> filePaths = new List<string>();
Image img = ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (img != null)
if (bmp != null)
{
List<Image> images = ImageHelpers.SplitImage(img, rowCount, columnCount);
List<Bitmap> images = ImageHelpers.SplitImage(bmp, rowCount, columnCount);
string originalFileName = Path.GetFileNameWithoutExtension(filePath);

View file

@ -166,11 +166,11 @@ private void btnGenerate_Click(object sender, EventArgs e)
if (File.Exists(filePath))
{
using (Image img = ImageHelpers.LoadImage(filePath))
using (Bitmap bmp = ImageHelpers.LoadImage(filePath))
{
if (img != null)
if (bmp != null)
{
using (Image thumbnail = ImageHelpers.CreateThumbnail(img, width, height))
using (Bitmap thumbnail = ImageHelpers.CreateThumbnail(bmp, width, height))
{
string filename = Path.GetFileNameWithoutExtension(filePath);
string outputPath = Path.Combine(outputFolder, outputFilename.Replace("$filename", filename));

View file

@ -50,7 +50,7 @@ private void LoadImageFile(string imageFilePath)
{
if (!string.IsNullOrEmpty(imageFilePath))
{
Image = (Bitmap)ImageHelpers.LoadImage(imageFilePath);
Image = ImageHelpers.LoadImage(imageFilePath);
if (Image != null)
{

View file

@ -69,13 +69,13 @@ private bool LoadImageFile(string filePath, bool centerImage)
{
if (!string.IsNullOrEmpty(filePath))
{
Image img = ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (img != null)
if (bmp != null)
{
AnnotationOptions.LastImageFilePath = filePath;
SetImage(img, centerImage);
SetImage(bmp, centerImage);
return true;
}

View file

@ -1608,8 +1608,8 @@ private void PasteFromClipboard(bool insertMousePosition)
if (!string.IsNullOrEmpty(imageFilePath))
{
Image img = ImageHelpers.LoadImage(imageFilePath);
InsertImage(img);
Bitmap bmp = ImageHelpers.LoadImage(imageFilePath);
InsertImage(bmp);
}
}
}
@ -1758,7 +1758,7 @@ private void LoadImageFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
Bitmap bmp = (Bitmap)ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (bmp != null)
{
@ -1780,8 +1780,8 @@ private void InsertImageFile()
if (!string.IsNullOrEmpty(filePath))
{
Image img = ImageHelpers.LoadImage(filePath);
InsertImage(img);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
InsertImage(bmp);
}
}
@ -1865,7 +1865,7 @@ private void ChangeImageSize()
if (size != oldSize)
{
InterpolationMode interpolationMode = GetInterpolationMode(Options.ImageEditorResizeInterpolationMode);
Bitmap bmp = (Bitmap)ImageHelpers.ResizeImage(Form.Canvas, size, interpolationMode);
Bitmap bmp = ImageHelpers.ResizeImage(Form.Canvas, size, interpolationMode);
if (bmp != null)
{

View file

@ -307,7 +307,7 @@ private void UpdateLayout()
}
}
public void UpdateThumbnail(Image image = null)
public void UpdateThumbnail(Bitmap bmp = null)
{
ClearThumbnail();
@ -323,11 +323,11 @@ public void UpdateThumbnail(Image image = null)
pbThumbnail.Cursor = Cursors.Hand;
}
Image img = CreateThumbnail(filePath, image);
Bitmap bmpResult = CreateThumbnail(filePath, bmp);
if (img != null)
if (bmpResult != null)
{
pbThumbnail.Image = img;
pbThumbnail.Image = bmpResult;
ThumbnailExists = true;
}
@ -339,11 +339,11 @@ public void UpdateThumbnail(Image image = null)
}
}
private Image CreateThumbnail(string filePath, Image image = null)
private Bitmap CreateThumbnail(string filePath, Bitmap bmp = null)
{
if (image != null)
if (bmp != null)
{
return ImageHelpers.ResizeImage(image, ThumbnailSize, false);
return ImageHelpers.ResizeImage(bmp, ThumbnailSize, false);
}
else
{
@ -353,11 +353,11 @@ private Image CreateThumbnail(string filePath, Image image = null)
}
else if (File.Exists(filePath))
{
using (Image img = ImageHelpers.LoadImage(filePath))
using (Bitmap bmpResult = ImageHelpers.LoadImage(filePath))
{
if (img != null)
if (bmpResult != null)
{
return ImageHelpers.ResizeImage(img, ThumbnailSize, false);
return ImageHelpers.ResizeImage(bmpResult, ThumbnailSize, false);
}
}
}
@ -365,9 +365,9 @@ private Image CreateThumbnail(string filePath, Image image = null)
if (!string.IsNullOrEmpty(filePath))
{
using (Icon icon = NativeMethods.GetJumboFileIcon(filePath, false))
using (Image img = icon.ToBitmap())
using (Bitmap bmpResult = icon.ToBitmap())
{
return ImageHelpers.ResizeImage(img, ThumbnailSize, false, true);
return ImageHelpers.ResizeImage(bmpResult, ThumbnailSize, false, true);
}
}
}

View file

@ -122,11 +122,11 @@ private void DecodeFromFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
using (Image img = ImageHelpers.LoadImage(filePath))
using (Bitmap bmp = ImageHelpers.LoadImage(filePath))
{
if (img != null)
if (bmp != null)
{
DecodeImage((Bitmap)img);
DecodeImage(bmp);
}
}
}

View file

@ -903,7 +903,7 @@ public static void AnnotateImageFromFile(string filePath, TaskSettings taskSetti
{
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
Bitmap bmp = (Bitmap)ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
AnnotateImageAsync(bmp, filePath, taskSettings);
}
@ -1031,7 +1031,7 @@ public static void OpenImageEffects(TaskSettings taskSettings = null)
if (!string.IsNullOrEmpty(filePath))
{
Bitmap bmp = (Bitmap)ImageHelpers.LoadImage(filePath);
Bitmap bmp = ImageHelpers.LoadImage(filePath);
if (bmp != null)
{

View file

@ -158,7 +158,7 @@ private static void Task_ImageReady(WorkerTask task, Image image)
if (Program.Settings.TaskViewMode == TaskViewMode.ThumbnailView)
{
panel.UpdateThumbnail(image);
panel.UpdateThumbnail((Bitmap)image);
}
}
}