mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-28 11:10:23 +12:00
TrimTransparent methods moved to ImageHelpers class as AutoCropImage
This commit is contained in:
parent
cab2ec0b2b
commit
56815698ec
|
@ -243,6 +243,159 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Automatically crop image to remove transparent outside area.</summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Automatically crop image to remove transparent outside area. Only checks center pixels.</summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Adds empty space around image.</summary>
|
/// <summary>Adds empty space around image.</summary>
|
||||||
public static Image AddCanvas(Image img, Padding margin)
|
public static Image AddCanvas(Image img, Padding margin)
|
||||||
{
|
{
|
||||||
|
|
|
@ -127,7 +127,7 @@ public Image CaptureWindowTransparent(IntPtr handle)
|
||||||
|
|
||||||
if (isTransparent)
|
if (isTransparent)
|
||||||
{
|
{
|
||||||
transparentImage = TrimTransparent(transparentImage);
|
transparentImage = ImageHelpers.AutoCropImage(transparentImage);
|
||||||
|
|
||||||
if (!CaptureShadow)
|
if (!CaptureShadow)
|
||||||
{
|
{
|
||||||
|
@ -202,160 +202,6 @@ private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackgr
|
||||||
return whiteBackground;
|
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)
|
private void TrimShadow(Bitmap bitmap)
|
||||||
{
|
{
|
||||||
int sizeLimit = 10;
|
int sizeLimit = 10;
|
||||||
|
|
Loading…
Reference in a new issue