How to create IntPtr to an array of bytes (byte[])

A

aaron.radich

I'm trying to create a bitmap that is defined from an array of bytes,
which are indexed to a ColorPalette. I'm trying to call the following
code, but the 5th parameter of the Bitmap constructor needs to be a
System.IntPtr. You can't pass by reference. How can I create an
IntPtr to the array of bytes (byte[])?

bmpBuffer = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT, 2,
System.Drawing.Imaging.PixelFormat.Format24bppRgb, ref
objCDG.CDG_ScreenBuffer);
bmpBuffer.Palette = objCDG.Palette;

Aaron
 
J

Jeroen Mostert

I'm trying to create a bitmap that is defined from an array of bytes,
which are indexed to a ColorPalette. I'm trying to call the following
code, but the 5th parameter of the Bitmap constructor needs to be a
System.IntPtr. You can't pass by reference. How can I create an
IntPtr to the array of bytes (byte[])?

bmpBuffer = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT, 2,
System.Drawing.Imaging.PixelFormat.Format24bppRgb, ref
objCDG.CDG_ScreenBuffer);
bmpBuffer.Palette = objCDG.Palette;
You can use System.Marshal to allocate unmanaged memory and copy the array:

IntPtr buffer = Marshal.AllocHGlobal(objCDG.CDG_ScreenBuffer.Length);
Marshal.Copy(objCDG.CDG_ScreenBuffer, 0, buffer,
objCDG.CDG_ScreenBuffer.Length);
bmpBuffer = new Bitmap(..., buffer);

Beware, this code doesn't free the memory, so you'll leak memory and this
will be noticeable if you do it a lot. The corresponding free call is
Marshal.FreeHGlobal(), but as Bitmap documents, you cannot free this memory
until you dispose of the Bitmap, which can be tough to impossible to arrange
if you have to hand it off to somewhere else.

A more convenient approach if your Bitmap is not a throwaway one is to use
Bitmap.LockBits(), which will give you access to the pixel array without
making you manage its memory. See the documentation on ImageLockMode for an
example.
 
A

aaron.radich

I'm trying to create a bitmap that is defined from an array of bytes,
which are indexed to a ColorPalette.  I'm trying to call the following
code, but the 5th parameter of the Bitmap constructor needs to be a
System.IntPtr.  You can't pass by reference.  How can I create an
IntPtr to the array of bytes (byte[])?
bmpBuffer = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT, 2,
System.Drawing.Imaging.PixelFormat.Format24bppRgb, ref
objCDG.CDG_ScreenBuffer);
bmpBuffer.Palette = objCDG.Palette;

You can use System.Marshal to allocate unmanaged memory and copy the array:

   IntPtr buffer = Marshal.AllocHGlobal(objCDG.CDG_ScreenBuffer.Length);
   Marshal.Copy(objCDG.CDG_ScreenBuffer, 0, buffer,
objCDG.CDG_ScreenBuffer.Length);
   bmpBuffer = new Bitmap(..., buffer);

Beware, this code doesn't free the memory, so you'll leak memory and this
will be noticeable if you do it a lot. The corresponding free call is
Marshal.FreeHGlobal(), but as Bitmap documents, you cannot free this memory
until you dispose of the Bitmap, which can be tough to impossible to arrange
if you have to hand it off to somewhere else.

A more convenient approach if your Bitmap is not a throwaway one is to use
Bitmap.LockBits(), which will give you access to the pixel array without
making you manage its memory. See the documentation on ImageLockMode for an
example.

Thanks. That appears to have worked. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top