/// <summary> /// Constructor. /// </summary> /// <param name="bitmap">Source image.</param> public unsafe RGBImage(BitmapSource bitmap) { int rows = bitmap.PixelHeight; int columns = bitmap.PixelWidth; _handler = new ByteArrayHandler(rows, columns, 3); FormatConvertedBitmap bmp = new FormatConvertedBitmap(); bmp.BeginInit(); bmp.Source = bitmap; bmp.DestinationFormat = PixelFormats.Rgb24; bmp.EndInit(); byte[] pixels = new byte[rows * columns * 3]; bmp.CopyPixels(pixels, columns * 3, 0); fixed(byte *src = pixels, dst = _handler.RawArray) { byte *srcPtr = src; byte *dstPtr = dst; int count = pixels.Length; while (count-- > 0) { *dstPtr++ = *srcPtr++; } } pixels = null; }
/// <summary> /// Constructor. /// </summary> /// <param name="bitmap">Source image.</param> public unsafe RGBImage(System.Drawing.Bitmap bitmap) { int r, c; byte *srcPtr, srcScan, dstPtr; int rows = bitmap.Height; int columns = bitmap.Width; _handler = new ByteArrayHandler(rows, columns, 3); System.Drawing.Imaging.BitmapData srcBuf = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); srcPtr = (byte *)srcBuf.Scan0; int srcStride = srcBuf.Stride; fixed(byte *dstBuf = _handler.RawArray) { dstPtr = dstBuf; for (r = 0; r < rows; r++, srcPtr += srcStride) { for (c = 0, srcScan = srcPtr; c < columns; c++) { byte B = *srcScan++; byte G = *srcScan++; byte R = *srcScan++; * dstPtr++ = R; * dstPtr++ = G; * dstPtr++ = B; } } } bitmap.UnlockBits(srcBuf); }
/// <summary> /// Constructor. /// </summary> /// <param name="rows">Number of rows</param> /// <param name="columns">Number of columns</param> public RGBImage(int rows, int columns) { _handler = new ByteArrayHandler(rows, columns, 3); }