Decrease unnecessary bitmap casts

This commit is contained in:
Jaex 2020-03-21 04:41:34 +03:00
parent f49c345ffd
commit b8c10a7067
13 changed files with 140 additions and 156 deletions

View file

@ -224,19 +224,6 @@ public static Image ResizeImageLimit(Image img, int size)
return ResizeImageLimit(img, size, size); return ResizeImageLimit(img, size, size);
} }
public static Image CropImage(Image img, Rectangle rect)
{
if (img != null && rect.X >= 0 && rect.Y >= 0 && rect.Width > 0 && rect.Height > 0 && new Rectangle(0, 0, img.Width, img.Height).Contains(rect))
{
using (Bitmap bmp = new Bitmap(img))
{
return bmp.Clone(rect, bmp.PixelFormat);
}
}
return null;
}
public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect) public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
{ {
if (bmp != null && rect.X >= 0 && rect.Y >= 0 && rect.Width > 0 && rect.Height > 0 && new Rectangle(0, 0, bmp.Width, bmp.Height).Contains(rect)) if (bmp != null && rect.X >= 0 && rect.Y >= 0 && rect.Width > 0 && rect.Height > 0 && new Rectangle(0, 0, bmp.Width, bmp.Height).Contains(rect))
@ -406,12 +393,12 @@ public static Bitmap QuickAutoCropTransparent(Bitmap bmp)
return bmp; return bmp;
} }
public static Image AddCanvas(Image img, Padding margin) public static Bitmap AddCanvas(Image img, Padding margin)
{ {
return AddCanvas(img, margin, Color.Transparent); return AddCanvas(img, margin, Color.Transparent);
} }
public static Image AddCanvas(Image img, Padding margin, Color canvasColor) public static Bitmap AddCanvas(Image img, Padding margin, Color canvasColor)
{ {
if (margin.All == 0 || img.Width + margin.Horizontal < 1 || img.Height + margin.Vertical < 1) if (margin.All == 0 || img.Width + margin.Horizontal < 1 || img.Height + margin.Vertical < 1)
{ {
@ -930,7 +917,7 @@ public static Bitmap AddShadow(Image img, float opacity, int size)
public static Bitmap AddShadow(Image img, float opacity, int size, float darkness, Color color, Point offset) public static Bitmap AddShadow(Image img, float opacity, int size, float darkness, Color color, Point offset)
{ {
Image shadowImage = null; Bitmap shadowImage = null;
try try
{ {
@ -950,7 +937,7 @@ public static Bitmap AddShadow(Image img, float opacity, int size, float darknes
if (size > 0) if (size > 0)
{ {
BoxBlur((Bitmap)shadowImage, size); BoxBlur(shadowImage, size);
} }
if (darkness > 1) if (darkness > 1)
@ -958,7 +945,7 @@ public static Bitmap AddShadow(Image img, float opacity, int size, float darknes
ColorMatrix alphaMatrix = new ColorMatrix(); ColorMatrix alphaMatrix = new ColorMatrix();
alphaMatrix.Matrix33 = darkness; alphaMatrix.Matrix33 = darkness;
Image shadowImage2 = alphaMatrix.Apply(shadowImage); Bitmap shadowImage2 = (Bitmap)alphaMatrix.Apply(shadowImage);
shadowImage.Dispose(); shadowImage.Dispose();
shadowImage = shadowImage2; shadowImage = shadowImage2;
} }
@ -981,16 +968,15 @@ public static Bitmap AddShadow(Image img, float opacity, int size, float darknes
} }
} }
public static Bitmap Sharpen(Image img, double strength) public static Bitmap Sharpen(Bitmap bmp, double strength)
{ {
using (Bitmap bitmap = (Bitmap)img) if (bmp != null)
{ {
if (bitmap != null) using (bmp)
{ {
Bitmap sharpenImage = bitmap.Clone() as Bitmap; Bitmap sharpenImage = (Bitmap)bmp.Clone();
int width = sharpenImage.Width;
int width = img.Width; int height = sharpenImage.Height;
int height = img.Height;
// Create sharpening filter. // Create sharpening filter.
const int filterSize = 5; const int filterSize = 5;
@ -1009,75 +995,73 @@ public static Bitmap Sharpen(Image img, double strength)
const int s = filterSize / 2; const int s = filterSize / 2;
Color[,] result = new Color[img.Width, img.Height]; Color[,] result = new Color[sharpenImage.Width, sharpenImage.Height];
// Lock image bits for read/write. // Lock image bits for read/write.
if (sharpenImage != null) BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride * height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = s; x < width - s; x++)
{ {
BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); for (int y = s; y < height - s; y++)
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride * height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = s; x < width - s; x++)
{ {
for (int y = s; y < height - s; y++) double red = 0.0, green = 0.0, blue = 0.0;
{
double red = 0.0, green = 0.0, blue = 0.0;
for (int filterX = 0; filterX < filterSize; filterX++) for (int filterX = 0; filterX < filterSize; filterX++)
{
for (int filterY = 0; filterY < filterSize; filterY++)
{ {
for (int filterY = 0; filterY < filterSize; filterY++) int imageX = (x - s + filterX + width) % width;
{ int imageY = (y - s + filterY + height) % height;
int imageX = (x - s + filterX + width) % width;
int imageY = (y - s + filterY + height) % height;
rgb = (imageY * pbits.Stride) + (3 * imageX); rgb = (imageY * pbits.Stride) + (3 * imageX);
red += rgbValues[rgb + 2] * filter[filterX, filterY]; red += rgbValues[rgb + 2] * filter[filterX, filterY];
green += rgbValues[rgb + 1] * filter[filterX, filterY]; green += rgbValues[rgb + 1] * filter[filterX, filterY];
blue += rgbValues[rgb + 0] * filter[filterX, filterY]; blue += rgbValues[rgb + 0] * filter[filterX, filterY];
}
rgb = (y * pbits.Stride) + (3 * x);
int r = Math.Min(Math.Max((int)((factor * red) + (bias * rgbValues[rgb + 2])), 0), 255);
int g = Math.Min(Math.Max((int)((factor * green) + (bias * rgbValues[rgb + 1])), 0), 255);
int b = Math.Min(Math.Max((int)((factor * blue) + (bias * rgbValues[rgb + 0])), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
} }
}
}
// Update the image with the sharpened pixels.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
rgb = (y * pbits.Stride) + (3 * x); rgb = (y * pbits.Stride) + (3 * x);
rgbValues[rgb + 2] = result[x, y].R; int r = Math.Min(Math.Max((int)((factor * red) + (bias * rgbValues[rgb + 2])), 0), 255);
rgbValues[rgb + 1] = result[x, y].G; int g = Math.Min(Math.Max((int)((factor * green) + (bias * rgbValues[rgb + 1])), 0), 255);
rgbValues[rgb + 0] = result[x, y].B; int b = Math.Min(Math.Max((int)((factor * blue) + (bias * rgbValues[rgb + 0])), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
} }
} }
// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
} }
// Update the image with the sharpened pixels.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
rgb = (y * pbits.Stride) + (3 * x);
rgbValues[rgb + 2] = result[x, y].R;
rgbValues[rgb + 1] = result[x, y].G;
rgbValues[rgb + 0] = result[x, y].B;
}
}
// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
return sharpenImage; return sharpenImage;
} }
} }
return null; return null;
} }
@ -1634,7 +1618,7 @@ public static string SaveImageFileDialog(Image img, string filePath = "", bool u
{ {
sfd.Filter = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tif, *.tiff)|*.tif;*.tiff"; sfd.Filter = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tif, *.tiff)|*.tif;*.tiff";
sfd.DefaultExt = "png"; sfd.DefaultExt = "png";
string initialDirectory = null; string initialDirectory = null;
if (useLastDirectory && !string.IsNullOrEmpty(HelpersOptions.LastSaveDirectory) && Directory.Exists(HelpersOptions.LastSaveDirectory)) if (useLastDirectory && !string.IsNullOrEmpty(HelpersOptions.LastSaveDirectory) && Directory.Exists(HelpersOptions.LastSaveDirectory))

View file

@ -59,7 +59,7 @@ public override Image Apply(Image img)
{ {
if (Margin.All == 0) return img; if (Margin.All == 0) return img;
return ImageHelpers.CropImage(img, new Rectangle(Margin.Left, Margin.Top, img.Width - Margin.Horizontal, img.Height - Margin.Vertical)); return ImageHelpers.CropBitmap((Bitmap)img, new Rectangle(Margin.Left, Margin.Top, img.Width - Margin.Horizontal, img.Height - Margin.Vertical));
} }
} }
} }

View file

@ -1285,7 +1285,7 @@ internal void UpdateRegionPath()
} }
} }
public Image GetResultImage() public Bitmap GetResultImage()
{ {
if (IsEditorMode) if (IsEditorMode)
{ {
@ -1304,9 +1304,9 @@ public Image GetResultImage()
gp = regionFillPath; gp = regionFillPath;
} }
using (Image img = RegionCaptureTasks.ApplyRegionPathToImage(Canvas, gp, out Rectangle rect)) using (Bitmap bmp = RegionCaptureTasks.ApplyRegionPathToImage(Canvas, gp, out Rectangle rect))
{ {
return ShapeManager.RenderOutputImage(img, rect.Location); return ShapeManager.RenderOutputImage(bmp, rect.Location);
} }
} }
else if (Result == RegionResult.Fullscreen) else if (Result == RegionResult.Fullscreen)
@ -1322,9 +1322,9 @@ public Image GetResultImage()
Screen screen = screens[MonitorIndex]; Screen screen = screens[MonitorIndex];
Rectangle screenRect = CaptureHelpers.ScreenToClient(screen.Bounds); Rectangle screenRect = CaptureHelpers.ScreenToClient(screen.Bounds);
using (Image img = ShapeManager.RenderOutputImage(Canvas)) using (Bitmap bmp = ShapeManager.RenderOutputImage(Canvas))
{ {
return ImageHelpers.CropImage(img, screenRect); return ImageHelpers.CropBitmap(bmp, screenRect);
} }
} }
} }
@ -1332,9 +1332,9 @@ public Image GetResultImage()
{ {
Rectangle activeScreenRect = CaptureHelpers.GetActiveScreenBounds0Based(); Rectangle activeScreenRect = CaptureHelpers.GetActiveScreenBounds0Based();
using (Image img = ShapeManager.RenderOutputImage(Canvas)) using (Bitmap bmp = ShapeManager.RenderOutputImage(Canvas))
{ {
return ImageHelpers.CropImage(img, activeScreenRect); return ImageHelpers.CropBitmap(bmp, activeScreenRect);
} }
} }

View file

@ -162,7 +162,7 @@ private void RectangleLight_MouseUp(object sender, MouseEventArgs e)
} }
} }
public Image GetAreaImage() public Bitmap GetAreaImage()
{ {
Rectangle rect = SelectionRectangle0Based; Rectangle rect = SelectionRectangle0Based;
@ -170,10 +170,10 @@ public Image GetAreaImage()
{ {
if (rect.X == 0 && rect.Y == 0 && rect.Width == backgroundImage.Width && rect.Height == backgroundImage.Height) if (rect.X == 0 && rect.Y == 0 && rect.Width == backgroundImage.Width && rect.Height == backgroundImage.Height)
{ {
return (Image)backgroundImage.Clone(); return (Bitmap)backgroundImage.Clone();
} }
return ImageHelpers.CropImage(backgroundImage, rect); return ImageHelpers.CropBitmap(backgroundImage, rect);
} }
return null; return null;

View file

@ -45,7 +45,7 @@ public partial class ScrollingCaptureForm : Form
private WindowInfo selectedWindow; private WindowInfo selectedWindow;
private Rectangle selectedRectangle; private Rectangle selectedRectangle;
private List<Image> images = new List<Image>(); private List<Bitmap> images = new List<Bitmap>();
private int currentScrollCount; private int currentScrollCount;
private bool isBusy, isCapturing, firstCapture, detectingScrollMethod; private bool isBusy, isCapturing, firstCapture, detectingScrollMethod;
private ScrollingCaptureScrollMethod currentScrollMethod; private ScrollingCaptureScrollMethod currentScrollMethod;
@ -244,11 +244,11 @@ private void Clean()
if (images != null) if (images != null)
{ {
foreach (Image image in images) foreach (Bitmap bmp in images)
{ {
if (image != null) if (bmp != null)
{ {
image.Dispose(); bmp.Dispose();
} }
} }
@ -279,13 +279,13 @@ private void RemoveDuplicates()
{ {
for (int i = images.Count - 1; i > 0; i--) for (int i = images.Count - 1; i > 0; i--)
{ {
bool result = ImageHelpers.IsImagesEqual((Bitmap)images[i], (Bitmap)images[i - 1]); bool result = ImageHelpers.IsImagesEqual(images[i], images[i - 1]);
if (result) if (result)
{ {
Image img = images[i]; Bitmap bmp = images[i];
images.Remove(img); images.Remove(bmp);
img.Dispose(); bmp.Dispose();
} }
} }
} }
@ -315,11 +315,11 @@ private void captureTimer_Tick(object sender, EventArgs e)
} }
Screenshot screenshot = new Screenshot() { CaptureCursor = false }; Screenshot screenshot = new Screenshot() { CaptureCursor = false };
Image image = screenshot.CaptureRectangle(selectedRectangle); Bitmap bmp = screenshot.CaptureRectangle(selectedRectangle);
if (image != null) if (bmp != null)
{ {
images.Add(image); images.Add(bmp);
} }
if (Options.ScrollMethod == ScrollingCaptureScrollMethod.Automatic && detectingScrollMethod && images.Count > 1 && IsLastTwoImagesSame()) if (Options.ScrollMethod == ScrollingCaptureScrollMethod.Automatic && detectingScrollMethod && images.Count > 1 && IsLastTwoImagesSame())
@ -433,11 +433,11 @@ private bool IsLastTwoImagesSame()
if (images.Count > 1) if (images.Count > 1)
{ {
result = ImageHelpers.IsImagesEqual((Bitmap)images[images.Count - 1], (Bitmap)images[images.Count - 2]); result = ImageHelpers.IsImagesEqual(images[images.Count - 1], images[images.Count - 2]);
if (result) if (result)
{ {
Image last = images[images.Count - 1]; Bitmap last = images[images.Count - 1];
images.Remove(last); images.Remove(last);
last.Dispose(); last.Dispose();
} }
@ -571,15 +571,15 @@ private Image CombineImages()
if (images.Count == 1) if (images.Count == 1)
{ {
return (Image)images[0].Clone(); return (Bitmap)images[0].Clone();
} }
List<Image> output = new List<Image>(); List<Bitmap> output = new List<Bitmap>();
for (int i = 0; i < images.Count - Options.IgnoreLast; i++) for (int i = 0; i < images.Count - Options.IgnoreLast; i++)
{ {
Image newImage; Bitmap newImage;
Image image = images[i]; Bitmap image = images[i];
if (Options.TrimLeftEdge > 0 || Options.TrimTopEdge > 0 || Options.TrimRightEdge > 0 || Options.TrimBottomEdge > 0 || if (Options.TrimLeftEdge > 0 || Options.TrimTopEdge > 0 || Options.TrimRightEdge > 0 || Options.TrimBottomEdge > 0 ||
Options.CombineAdjustmentVertical > 0 || Options.CombineAdjustmentLastVertical > 0) Options.CombineAdjustmentVertical > 0 || Options.CombineAdjustmentLastVertical > 0)
@ -598,7 +598,7 @@ private Image CombineImages()
rect.Height -= Options.CombineAdjustmentVertical; rect.Height -= Options.CombineAdjustmentVertical;
} }
newImage = ImageHelpers.CropImage(image, rect); newImage = ImageHelpers.CropBitmap(image, rect);
if (newImage == null) if (newImage == null)
{ {
@ -607,7 +607,7 @@ private Image CombineImages()
} }
else else
{ {
newImage = (Image)image.Clone(); newImage = (Bitmap)image.Clone();
} }
output.Add(newImage); output.Add(newImage);
@ -615,7 +615,7 @@ private Image CombineImages()
Image result = ImageHelpers.CombineImages(output); Image result = ImageHelpers.CombineImages(output);
foreach (Image image in output) foreach (Bitmap image in output)
{ {
if (image != null) if (image != null)
{ {
@ -664,13 +664,13 @@ private void chkAutoUpload_CheckedChanged(object sender, EventArgs e)
Options.AutoUpload = chkAutoUpload.Checked; Options.AutoUpload = chkAutoUpload.Checked;
} }
private Padding GuessEdges(Image img1, Image img2) private Padding GuessEdges(Bitmap img1, Bitmap img2)
{ {
Padding result = new Padding(); Padding result = new Padding();
Rectangle rect = new Rectangle(0, 0, img1.Width, img1.Height); Rectangle rect = new Rectangle(0, 0, img1.Width, img1.Height);
using (UnsafeBitmap bmp1 = new UnsafeBitmap((Bitmap)img1, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap bmp1 = new UnsafeBitmap(img1, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap bmp2 = new UnsafeBitmap((Bitmap)img2, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap bmp2 = new UnsafeBitmap(img2, true, ImageLockMode.ReadOnly))
{ {
bool valueFound = false; bool valueFound = false;
@ -761,7 +761,7 @@ private void GuessCombineAdjustments()
} }
} }
private int CalculateVerticalOffset(Image img1, Image img2, int ignoreRightOffset = 50) private int CalculateVerticalOffset(Bitmap img1, Bitmap img2, int ignoreRightOffset = 50)
{ {
int lastMatchCount = 0; int lastMatchCount = 0;
int lastMatchOffset = 0; int lastMatchOffset = 0;
@ -770,8 +770,8 @@ private int CalculateVerticalOffset(Image img1, Image img2, int ignoreRightOffse
img1.Width - Options.TrimLeftEdge - Options.TrimRightEdge - (img1.Width > ignoreRightOffset ? ignoreRightOffset : 0), img1.Width - Options.TrimLeftEdge - Options.TrimRightEdge - (img1.Width > ignoreRightOffset ? ignoreRightOffset : 0),
img1.Height - Options.TrimTopEdge - Options.TrimBottomEdge); img1.Height - Options.TrimTopEdge - Options.TrimBottomEdge);
using (UnsafeBitmap bmp1 = new UnsafeBitmap((Bitmap)img1, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap bmp1 = new UnsafeBitmap(img1, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap bmp2 = new UnsafeBitmap((Bitmap)img2, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap bmp2 = new UnsafeBitmap(img2, true, ImageLockMode.ReadOnly))
{ {
for (int y = rect.Y; y < rect.Bottom; y++) for (int y = rect.Y; y < rect.Bottom; y++)
{ {

View file

@ -32,7 +32,7 @@ namespace ShareX.ScreenCaptureLib
{ {
public static class RegionCaptureTasks public static class RegionCaptureTasks
{ {
public static Image GetRegionImage(RegionCaptureOptions options) public static Bitmap GetRegionImage(RegionCaptureOptions options)
{ {
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options); RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
@ -158,9 +158,9 @@ public static void ShowScreenRuler(RegionCaptureOptions options)
} }
} }
public static Image ApplyRegionPathToImage(Image img, GraphicsPath gp, out Rectangle resultArea) public static Bitmap ApplyRegionPathToImage(Bitmap bmp, GraphicsPath gp, out Rectangle resultArea)
{ {
if (img != null && gp != null) if (bmp != null && gp != null)
{ {
Rectangle regionArea = Rectangle.Round(gp.GetBounds()); Rectangle regionArea = Rectangle.Round(gp.GetBounds());
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based(); Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
@ -168,16 +168,16 @@ public static Image ApplyRegionPathToImage(Image img, GraphicsPath gp, out Recta
if (resultArea.IsValid()) if (resultArea.IsValid())
{ {
using (Bitmap bmp = img.CreateEmptyBitmap()) using (Bitmap bmpResult = bmp.CreateEmptyBitmap())
using (Graphics g = Graphics.FromImage(bmp)) using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(img)) using (TextureBrush brush = new TextureBrush(bmp))
{ {
g.PixelOffsetMode = PixelOffsetMode.Half; g.PixelOffsetMode = PixelOffsetMode.Half;
g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality;
g.FillPath(brush, gp); g.FillPath(brush, gp);
return ImageHelpers.CropBitmap(bmp, resultArea); return ImageHelpers.CropBitmap(bmpResult, resultArea);
} }
} }
} }

View file

@ -93,17 +93,17 @@ public Image CaptureWindowTransparent(IntPtr handle)
Thread.Sleep(10); Thread.Sleep(10);
Application.DoEvents(); Application.DoEvents();
whiteBackground = (Bitmap)CaptureRectangleNative(rect); whiteBackground = CaptureRectangleNative(rect);
form.BackColor = Color.Black; form.BackColor = Color.Black;
Application.DoEvents(); Application.DoEvents();
blackBackground = (Bitmap)CaptureRectangleNative(rect); blackBackground = CaptureRectangleNative(rect);
form.BackColor = Color.White; form.BackColor = Color.White;
Application.DoEvents(); Application.DoEvents();
whiteBackground2 = (Bitmap)CaptureRectangleNative(rect); whiteBackground2 = CaptureRectangleNative(rect);
form.Close(); form.Close();
} }

View file

@ -37,7 +37,7 @@ public abstract class BaseEffectShape : BaseShape
public abstract string OverlayText { get; } public abstract string OverlayText { get; }
private bool drawCache, isEffectCaching, isCachePending, isDisposePending; private bool drawCache, isEffectCaching, isCachePending, isDisposePending;
private Image cachedEffect; private Bitmap cachedEffect;
public abstract void ApplyEffect(Bitmap bmp); public abstract void ApplyEffect(Bitmap bmp);
@ -155,7 +155,7 @@ private void CacheEffect()
Task.Run(() => Task.Run(() =>
{ {
ApplyEffect((Bitmap)cachedEffect); ApplyEffect(cachedEffect);
isEffectCaching = false; isEffectCaching = false;

View file

@ -1261,14 +1261,14 @@ public WindowInfo FindSelectedWindowInfo(Point position)
return null; return null;
} }
public Image RenderOutputImage(Image img) public Bitmap RenderOutputImage(Bitmap bmp)
{ {
return RenderOutputImage(img, Point.Empty); return RenderOutputImage(bmp, Point.Empty);
} }
public Image RenderOutputImage(Image img, Point offset) public Bitmap RenderOutputImage(Bitmap bmp, Point offset)
{ {
Bitmap bmp = new Bitmap(img); Bitmap bmpOutput = (Bitmap)bmp.Clone();
if (DrawingShapes.Length > 0 || EffectShapes.Length > 0) if (DrawingShapes.Length > 0 || EffectShapes.Length > 0)
{ {
@ -1277,13 +1277,13 @@ public Image RenderOutputImage(Image img, Point offset)
MoveAll(-offset.X, -offset.Y); MoveAll(-offset.X, -offset.Y);
using (Graphics g = Graphics.FromImage(bmp)) using (Graphics g = Graphics.FromImage(bmpOutput))
{ {
foreach (BaseEffectShape shape in EffectShapes) foreach (BaseEffectShape shape in EffectShapes)
{ {
if (shape != null) if (shape != null)
{ {
shape.OnDrawFinal(g, bmp); shape.OnDrawFinal(g, bmpOutput);
} }
} }
@ -1302,7 +1302,7 @@ public Image RenderOutputImage(Image img, Point offset)
IsRenderingOutput = false; IsRenderingOutput = false;
} }
return bmp; return bmpOutput;
} }
private void SelectShape(BaseShape shape) private void SelectShape(BaseShape shape)
@ -1787,7 +1787,7 @@ private void InsertImageFile()
private void InsertImageFromScreen() private void InsertImageFromScreen()
{ {
Image img; Bitmap bmp;
try try
{ {
@ -1796,7 +1796,7 @@ private void InsertImageFromScreen()
menuForm.Hide(); menuForm.Hide();
Thread.Sleep(250); Thread.Sleep(250);
img = RegionCaptureTasks.GetRegionImage(Options); bmp = RegionCaptureTasks.GetRegionImage(Options);
} }
finally finally
{ {
@ -1805,7 +1805,7 @@ private void InsertImageFromScreen()
Form.Resume(); Form.Resume();
} }
InsertImage(img); InsertImage(bmp);
} }
private void InsertImage(Image img) private void InsertImage(Image img)
@ -1905,7 +1905,7 @@ private void ChangeCanvasSize()
if (canvasSizeForm.ShowDialog(Form) == DialogResult.OK) if (canvasSizeForm.ShowDialog(Form) == DialogResult.OK)
{ {
Padding canvas = canvasSizeForm.Canvas; Padding canvas = canvasSizeForm.Canvas;
Bitmap bmp = (Bitmap)ImageHelpers.AddCanvas(Form.Canvas, canvas, Options.EditorCanvasColor); Bitmap bmp = ImageHelpers.AddCanvas(Form.Canvas, canvas, Options.EditorCanvasColor);
if (bmp != null) if (bmp != null)
{ {
@ -1933,7 +1933,7 @@ public void AutoResizeCanvas()
private void ChangeCanvasSize(Padding margin, Color canvasColor) private void ChangeCanvasSize(Padding margin, Color canvasColor)
{ {
Bitmap bmp = (Bitmap)ImageHelpers.AddCanvas(Form.Canvas, margin, canvasColor); Bitmap bmp = ImageHelpers.AddCanvas(Form.Canvas, margin, canvasColor);
if (bmp != null) if (bmp != null)
{ {

View file

@ -39,10 +39,10 @@ protected override ImageInfo Execute(TaskSettings taskSettings)
case RegionCaptureType.Default: case RegionCaptureType.Default:
if (RegionCaptureForm.LastRegionFillPath != null) if (RegionCaptureForm.LastRegionFillPath != null)
{ {
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen()) using (Bitmap screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{ {
Image img = RegionCaptureTasks.ApplyRegionPathToImage(screenshot, RegionCaptureForm.LastRegionFillPath, out _); Bitmap bmp = RegionCaptureTasks.ApplyRegionPathToImage(screenshot, RegionCaptureForm.LastRegionFillPath, out _);
return new ImageInfo(img); return new ImageInfo(bmp);
} }
} }
else else
@ -52,10 +52,10 @@ protected override ImageInfo Execute(TaskSettings taskSettings)
case RegionCaptureType.Light: case RegionCaptureType.Light:
if (!RegionCaptureLightForm.LastSelectionRectangle0Based.IsEmpty) if (!RegionCaptureLightForm.LastSelectionRectangle0Based.IsEmpty)
{ {
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen()) using (Bitmap screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{ {
Image img = ImageHelpers.CropImage(screenshot, RegionCaptureLightForm.LastSelectionRectangle0Based); Bitmap bmp = ImageHelpers.CropBitmap(screenshot, RegionCaptureLightForm.LastSelectionRectangle0Based);
return new ImageInfo(img); return new ImageInfo(bmp);
} }
} }
else else
@ -65,10 +65,10 @@ protected override ImageInfo Execute(TaskSettings taskSettings)
case RegionCaptureType.Transparent: case RegionCaptureType.Transparent:
if (!RegionCaptureTransparentForm.LastSelectionRectangle0Based.IsEmpty) if (!RegionCaptureTransparentForm.LastSelectionRectangle0Based.IsEmpty)
{ {
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen()) using (Bitmap screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{ {
Image img = ImageHelpers.CropImage(screenshot, RegionCaptureTransparentForm.LastSelectionRectangle0Based); Bitmap bmp = ImageHelpers.CropBitmap(screenshot, RegionCaptureTransparentForm.LastSelectionRectangle0Based);
return new ImageInfo(img); return new ImageInfo(bmp);
} }
} }
else else

View file

@ -118,22 +118,22 @@ protected ImageInfo ExecuteRegionCapture(TaskSettings taskSettings)
protected ImageInfo ExecuteRegionCaptureLight(TaskSettings taskSettings) protected ImageInfo ExecuteRegionCaptureLight(TaskSettings taskSettings)
{ {
Image img = null; Bitmap bmp = null;
using (RegionCaptureLightForm rectangleLight = new RegionCaptureLightForm(TaskHelpers.GetScreenshot(taskSettings))) using (RegionCaptureLightForm rectangleLight = new RegionCaptureLightForm(TaskHelpers.GetScreenshot(taskSettings)))
{ {
if (rectangleLight.ShowDialog() == DialogResult.OK) if (rectangleLight.ShowDialog() == DialogResult.OK)
{ {
img = rectangleLight.GetAreaImage(); bmp = rectangleLight.GetAreaImage();
if (img != null) if (bmp != null)
{ {
lastRegionCaptureType = RegionCaptureType.Light; lastRegionCaptureType = RegionCaptureType.Light;
} }
} }
} }
return new ImageInfo(img); return new ImageInfo(bmp);
} }
protected ImageInfo ExecuteRegionCaptureTransparent(TaskSettings taskSettings) protected ImageInfo ExecuteRegionCaptureTransparent(TaskSettings taskSettings)

View file

@ -218,11 +218,11 @@ private void btnDecodeFromScreen_Click(object sender, EventArgs e)
TaskSettings taskSettings = TaskSettings.GetDefaultTaskSettings(); TaskSettings taskSettings = TaskSettings.GetDefaultTaskSettings();
using (Image img = RegionCaptureTasks.GetRegionImage(taskSettings.CaptureSettings.SurfaceOptions)) using (Bitmap bmp = RegionCaptureTasks.GetRegionImage(taskSettings.CaptureSettings.SurfaceOptions))
{ {
if (img != null) if (bmp != null)
{ {
DecodeImage((Bitmap)img); DecodeImage(bmp);
} }
} }
} }

View file

@ -1011,7 +1011,7 @@ public static Bitmap AnnotateImage(Bitmap bmp, string filePath, TaskSettings tas
return null; return null;
case RegionResult.Region: // Enter case RegionResult.Region: // Enter
case RegionResult.AnnotateRunAfterCaptureTasks: case RegionResult.AnnotateRunAfterCaptureTasks:
return (Bitmap)form.GetResultImage(); return form.GetResultImage();
case RegionResult.Fullscreen: // Space or right click case RegionResult.Fullscreen: // Space or right click
case RegionResult.AnnotateContinueTask: case RegionResult.AnnotateContinueTask:
return (Bitmap)form.Canvas.Clone(); return (Bitmap)form.Canvas.Clone();
@ -1123,9 +1123,9 @@ public static async Task OCRImage(TaskSettings taskSettings = null)
{ {
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
using (Image img = RegionCaptureTasks.GetRegionImage(taskSettings.CaptureSettings.SurfaceOptions)) using (Bitmap bmp = RegionCaptureTasks.GetRegionImage(taskSettings.CaptureSettings.SurfaceOptions))
{ {
await OCRImage(img, taskSettings); await OCRImage(bmp, taskSettings);
} }
} }
} }