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);