With C # (I know that you are using a sample in VB.Net), you can use insecure and very quick access to your bitmap:
const int ALPHA_PIXEL = 3; const int RED_PIXEL = 2; const int GREEN_PIXEL = 1; const int BLUE_PIXEL = 0; try { BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); int bytesPerPixel = (bmp.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4); int stride = bmData.Stride; unsafe { byte* pixel = (byte*)(void*)bmData.Scan0; for (int y = 0; y < bmp.Height; y++) { int yPos = y * stride; for (int x = 0; x < bmp.Width; x++) { int pos = yPos + (x * bytesPerPixel); if (pixel[pos + RED_PIXEL] != 255 || pixel[pos + GREEN_PIXEL] != 255 || pixel[pos + BLUE_PIXEL] != 255 || pixel[pos + ALPHA_PIXEL] != 0) { return true; } } } } } finally { bmp.UnlockBits(bmData); } return false;
If you cannot use C #, then the fastest way would be to use Marshal.Copy as a block:
Dim bmData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat) Try Dim bytesPerPixel As Integer = If(bmp.PixelFormat = PixelFormat.Format24bppRgb, 3, 4) Dim stride As Integer = bmData.Stride Dim imageSize As Integer = stride * bmp.Height Dim pixel As Byte() ReDim pixel(imageSize) Marshal.Copy(bmData.Scan0, pixel, 0, imageSize) For y As Integer = 0 To bmp.Height - 1 Dim yPos As Integer = y * stride For x As Integer = 0 To bmp.Width - 1 Dim pos As Integer = yPos + (x * bytesPerPixel) If pixel(pos + RED_PIXEL) <> 255 OrElse pixel(pos + GREEN_PIXEL) <> 255 OrElse pixel(pos + BLUE_PIXEL) <> 255 OrElse pixel(pos + ALPHA_PIXEL) <> 0 Then Return End If Next Next Finally bmp.UnlockBits(bmData) End Try Return
source share