private static 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); }
public static bool Compare(UnsafeBitmap bmp1, UnsafeBitmap bmp2) { int pixelCount = bmp1.PixelCount; if (pixelCount != bmp2.PixelCount) { return(false); } if (!bmp1.IsLocked) { bmp1.Lock(ImageLockMode.ReadOnly); } if (!bmp2.IsLocked) { bmp2.Lock(ImageLockMode.ReadOnly); } ColorBgra *pointer1 = bmp1.Pointer; ColorBgra *pointer2 = bmp2.Pointer; for (int i = 0; i < pixelCount; i++) { if (pointer1->Bgra != pointer2->Bgra) { return(false); } pointer1++; pointer2++; } return(true); }
private static bool IsImagesEqual(Bitmap bmp1, Bitmap bmp2) { using (UnsafeBitmap unsafeBitmap1 = new UnsafeBitmap(bmp1)) using (UnsafeBitmap unsafeBitmap2 = new UnsafeBitmap(bmp2)) { return(unsafeBitmap1 == unsafeBitmap2); } }
private static 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 static 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 static 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 static 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 static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground) { if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size) { Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb); using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int pixelCount = blackBitmap.PixelCount; for (int i = 0; i < pixelCount; i++) { ColorBgra white = whiteBitmap.GetPixel(i); ColorBgra black = blackBitmap.GetPixel(i); double alpha = (black.Red - white.Red + 255) / 255.0; if (alpha == 1) { resultBitmap.SetPixel(i, white); } else if (alpha > 0) { white.Blue = (byte)(black.Blue / alpha); white.Green = (byte)(black.Green / alpha); white.Red = (byte)(black.Red / alpha); white.Alpha = (byte)(255 * alpha); resultBitmap.SetPixel(i, white); } } } return(result); } return(whiteBackground); }
private static void TrimShadow(Bitmap bitmap) { int sizeLimit = 10; int alphaLimit = 200; using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true)) { for (int i = 0; i < sizeLimit; i++) { int y = i; int width = bitmap.Width; // Left top for (int x = 0; x < sizeLimit; x++) { if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit) { unsafeBitmap.ClearPixel(x, y); } else { break; } } // Right top for (int x = width - 1; x > width - sizeLimit - 1; x--) { if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit) { unsafeBitmap.ClearPixel(x, y); } else { break; } } y = bitmap.Height - i - 1; // Left bottom for (int x = 0; x < sizeLimit; x++) { if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit) { unsafeBitmap.ClearPixel(x, y); } else { break; } } // Right bottom for (int x = width - 1; x > width - sizeLimit - 1; x--) { if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit) { unsafeBitmap.ClearPixel(x, y); } else { break; } } } } }
public static bool Compare(UnsafeBitmap bmp1, UnsafeBitmap bmp2) { int pixelCount = bmp1.PixelCount; if (pixelCount != bmp2.PixelCount) return false; if (!bmp1.IsLocked) bmp1.Lock(ImageLockMode.ReadOnly); if (!bmp2.IsLocked) bmp2.Lock(ImageLockMode.ReadOnly); ColorBgra* pointer1 = bmp1.Pointer; ColorBgra* pointer2 = bmp2.Pointer; for (int i = 0; i < pixelCount; i++) { if (pointer1->Bgra != pointer2->Bgra) return false; pointer1++; pointer2++; } return true; }