diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index c6f6d4b32..8f306b7ab 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -243,6 +243,159 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect) return null; } + /// Automatically crop image to remove transparent outside area. + public static Bitmap AutoCropImage(Bitmap bmp) + { + Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height); + Rectangle rect = source; + + using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly)) + { + bool leave = false; + + // Find X + for (int x = rect.X; x < rect.Width && !leave; x++) + { + for (int y = rect.Y; y < rect.Height; y++) + { + if (unsafeBitmap.GetPixel(x, y).Alpha > 0) + { + rect.X = x; + leave = true; + break; + } + } + } + + leave = false; + + // Find Y + for (int y = rect.Y; y < rect.Height && !leave; y++) + { + for (int x = rect.X; x < rect.Width; x++) + { + if (unsafeBitmap.GetPixel(x, y).Alpha > 0) + { + rect.Y = y; + leave = true; + break; + } + } + } + + leave = false; + + // Find Width + for (int x = rect.Width - 1; x >= rect.X && !leave; x--) + { + for (int y = rect.Y; y < rect.Height; y++) + { + if (unsafeBitmap.GetPixel(x, y).Alpha > 0) + { + rect.Width = x - rect.X + 1; + leave = true; + break; + } + } + } + + leave = false; + + // Find Height + for (int y = rect.Height - 1; y >= rect.Y && !leave; y--) + { + for (int x = rect.X; x < rect.Width; x++) + { + if (unsafeBitmap.GetPixel(x, y).Alpha > 0) + { + rect.Height = y - rect.Y + 1; + leave = true; + break; + } + } + } + } + + if (source != rect) + { + Bitmap croppedBitmap = CropBitmap(bmp, rect); + + if (croppedBitmap != null) + { + bmp.Dispose(); + return croppedBitmap; + } + } + + return bmp; + } + + /// Automatically crop image to remove transparent outside area. Only checks center pixels. + public static Bitmap QuickAutoCropImage(Bitmap bmp) + { + Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height); + Rectangle rect = source; + + using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly)) + { + int middleX = rect.Width / 2; + int middleY = rect.Height / 2; + + // Find X + for (int x = rect.X; x < rect.Width; x++) + { + if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0) + { + rect.X = x; + break; + } + } + + // Find Y + for (int y = rect.Y; y < rect.Height; y++) + { + if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0) + { + rect.Y = y; + break; + } + } + + // Find Width + for (int x = rect.Width - 1; x >= rect.X; x--) + { + if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0) + { + rect.Width = x - rect.X + 1; + break; + } + } + + // Find Height + for (int y = rect.Height - 1; y >= rect.Y; y--) + { + if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0) + { + rect.Height = y - rect.Y + 1; + break; + } + } + } + + if (source != rect) + { + Bitmap croppedBitmap = CropBitmap(bmp, rect); + + if (croppedBitmap != null) + { + bmp.Dispose(); + return croppedBitmap; + } + } + + return bmp; + } + /// Adds empty space around image. public static Image AddCanvas(Image img, Padding margin) { diff --git a/ShareX.ScreenCaptureLib/Screenshot_Transparent.cs b/ShareX.ScreenCaptureLib/Screenshot_Transparent.cs index db31e67d3..27dda9738 100644 --- a/ShareX.ScreenCaptureLib/Screenshot_Transparent.cs +++ b/ShareX.ScreenCaptureLib/Screenshot_Transparent.cs @@ -127,7 +127,7 @@ public Image CaptureWindowTransparent(IntPtr handle) if (isTransparent) { - transparentImage = TrimTransparent(transparentImage); + transparentImage = ImageHelpers.AutoCropImage(transparentImage); if (!CaptureShadow) { @@ -202,160 +202,6 @@ private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackgr return whiteBackground; } - private Bitmap TrimTransparent(Bitmap bitmap) - { - Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height); - Rectangle rect = source; - - using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly)) - { - rect = TrimTransparentFindX(unsafeBitmap, rect); - rect = TrimTransparentFindY(unsafeBitmap, rect); - rect = TrimTransparentFindWidth(unsafeBitmap, rect); - rect = TrimTransparentFindHeight(unsafeBitmap, rect); - } - - if (source != rect) - { - Bitmap croppedBitmap = ImageHelpers.CropBitmap(bitmap, rect); - - if (croppedBitmap != null) - { - bitmap.Dispose(); - return croppedBitmap; - } - } - - return bitmap; - } - - private Rectangle TrimTransparentFindX(UnsafeBitmap unsafeBitmap, Rectangle rect) - { - for (int x = rect.X; x < rect.Width; x++) - { - for (int y = rect.Y; y < rect.Height; y++) - { - if (unsafeBitmap.GetPixel(x, y).Alpha > 0) - { - rect.X = x; - return rect; - } - } - } - - return rect; - } - - private Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect) - { - for (int y = rect.Y; y < rect.Height; y++) - { - for (int x = rect.X; x < rect.Width; x++) - { - if (unsafeBitmap.GetPixel(x, y).Alpha > 0) - { - rect.Y = y; - return rect; - } - } - } - - return rect; - } - - private Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect) - { - for (int x = rect.Width - 1; x >= rect.X; x--) - { - for (int y = rect.Y; y < rect.Height; y++) - { - if (unsafeBitmap.GetPixel(x, y).Alpha > 0) - { - rect.Width = x - rect.X + 1; - return rect; - } - } - } - - return rect; - } - - private Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect) - { - for (int y = rect.Height - 1; y >= rect.Y; y--) - { - for (int x = rect.X; x < rect.Width; x++) - { - if (unsafeBitmap.GetPixel(x, y).Alpha > 0) - { - rect.Height = y - rect.Y + 1; - return rect; - } - } - } - - return rect; - } - - private Bitmap QuickTrimTransparent(Bitmap bitmap) - { - Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height); - Rectangle rect = source; - - using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly)) - { - int middleX = rect.Width / 2; - int middleY = rect.Height / 2; - - // Find X - for (int x = rect.X; x < rect.Width; x++) - { - if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0) - { - rect.X = x; - break; - } - } - - // Find Y - for (int y = rect.Y; y < rect.Height; y++) - { - if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0) - { - rect.Y = y; - break; - } - } - - // Find Width - for (int x = rect.Width - 1; x >= rect.X; x--) - { - if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0) - { - rect.Width = x - rect.X + 1; - break; - } - } - - // Find Height - for (int y = rect.Height - 1; y >= rect.Y; y--) - { - if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0) - { - rect.Height = y - rect.Y + 1; - break; - } - } - } - - if (source != rect) - { - return ImageHelpers.CropBitmap(bitmap, rect); - } - - return bitmap; - } - private void TrimShadow(Bitmap bitmap) { int sizeLimit = 10;