public static void DrawBitmapToBuffer(Bitmap source, PixelsBuffer buffer) { //make sure we have a ABGR Bitmap, same as PixelFormat.Format32bppArgb var bmp = ConvertConfig(source, Bitmap.Config.Argb8888); //copy Bitmap's data to buffer IntPtr ptr = bmp.LockPixels(); int size = bmp.RowBytes * bmp.Height; byte[] tmpBuffer = new byte[bmp.Width * bmp.Height * 4]; System.Runtime.InteropServices.Marshal.Copy(ptr, tmpBuffer, 0, size); bmp.UnlockPixels(); //copy to pixel buffer buffer.FromBytes(tmpBuffer, PixelFormats.Bgra); }
/// <summary> /// Draw a gdi+ System.Drawing.Bitmap to the rendering buffer /// </summary> public static void DrawBitmapToBuffer(Image source, PixelsBuffer buffer) { //make sure we have a ABGR System.Drawing.Bitmap Bitmap bmp = null; bmp = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height)); g.Dispose(); //copy System.Drawing.Bitmap's data to buffer Rectangle r = new Rectangle(0, 0, source.Width, source.Height); BitmapData data = bmp.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); IntPtr ptr = data.Scan0; int size = data.Stride * bmp.Height; byte[] tmpBuffer = new byte[bmp.Width * bmp.Height * 4]; System.Runtime.InteropServices.Marshal.Copy(ptr, tmpBuffer, 0, size); bmp.UnlockBits(data); //copy to pixel buffer buffer.FromBytes(tmpBuffer, PixelFormats.Bgra); }