I have an array like byte[] pixels. Is there any way to create a bitmap object from that pixels without copying data? I have a small graphics library, when I need to display image on WinForms window I just copy that data to a bitmap object, then I use draw method. Can I avoid this copying process? I remember I saw it somewhere, but maybe my memory is just bad.
Edit: I tried this code and it works, but is this safe?
byte[] pixels = new byte[10 * 10 * 4];
pixels[4] = 255; // set 1 pixel
pixels[5] = 255;
pixels[6] = 255;
pixels[7] = 255;
// do some tricks
GCHandle pinnedArray = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// create a new bitmap.
Bitmap bmp = new Bitmap (10, 10, 4*10, PixelFormat.Format32bppRgb, pointer);
Graphics grp = this.CreateGraphics ();
grp.DrawImage (bmp, 0, 0);
pixels[4+12] = 255; // add a pixel
pixels[5+12] = 255;
pixels[6+12] = 255;
pixels[7+12] = 255;
grp.DrawImage (bmp, 0, 40);
There is a constructor that takes a pointer to raw image data:
Bitmap Constructor (Int32, Int32, Int32, PixelFormat, IntPtr)
Example:
byte[] _data = new byte[]
{
255, 0, 0, 255, // Blue
0, 255, 0, 255, // Green
0, 0, 255, 255, // Red
0, 0, 0, 255, // Black
};
var arrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(_data,
System.Runtime.InteropServices.GCHandleType.Pinned);
var bmp = new Bitmap(2, 2, // 2x2 pixels
8, // RGB32 => 8 bytes stride
System.Drawing.Imaging.PixelFormat.Format32bppArgb,
arrayHandle.AddrOfPinnedObject()
);
this.BackgroundImageLayout = ImageLayout.Stretch;
this.BackgroundImage = bmp;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With