private 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; } } } } }
private static void TrimShadow(Bitmap bitmap) { int sizeLimit = 10; int alphaLimit = 200; using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadWrite)) { 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; } } } } }