public static void Read32BitImage(byte[] intermediateImage, BitmapSource src, int srcX, int srcY, int stride, int width, int height) { System.Diagnostics.Debug.Assert(src.Format.BitsPerPixel == 32); int[] pix = new int[width * height]; src.CopyPixels(new Int32Rect(srcX, srcY, width, height), pix, width * sizeof(int), 0); int si = 0; int di0 = 0; for (int y = 0; y < height; ++y, di0 += stride) { int di = di0; for (int x = 0; x < width; ++x, ++si, ++di) { int p = pix[si]; intermediateImage[di] = BadgeImage.ToGray((p >> 16) & 0xFF, (p >> 8) & 0xFF, (p >> 0) & 0xFF); } } }
public static void ReadBitmap(byte[] intermediateImage, Bitmap bitmap, int srcX, int srcY, int stride, int width, int height) { var data = bitmap.LockBits( new System.Drawing.Rectangle(srcX, srcY, width, height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); try { IntPtr p0 = data.Scan0; for (int y = 0, i0 = 0; y < height; ++y, p0 += data.Stride, i0 += stride) { IntPtr p = p0; for (int x = 0, i = i0; x < width; ++x, ++i, p += 4) { int val = Marshal.ReadInt32(p); intermediateImage[i] = BadgeImage.ToGray((val >> 16) & 0xFF, (val >> 8) & 0xFF, (val >> 0) & 0xFF); } } } finally { bitmap.UnlockBits(data); } }