I'm having trouble converting an unsigned char* into a Bitmap image. I have an unsigned char* in managed c++ code, which points to a 1280x1024 image with 8bppIndexed as the pixel format. When I display the image using the raw unsigned char* I'm getting the right image.

In order to display the image on a C# console window, I need to convert the image from my C++ code in a System:Big Smilerawing::Bitmap object. To do this, I have attempted to cast the pointer to IntPtr:

IntPtr a_image_data = (IntPtr)const_cast<unsigned char*>( image_data ); //image_data is the unsigned char*

I then used a delegate and event pair to send this IntPtr to the C# side of my project, and it retains the same IntPtr value. When I recreate the image using the IntPtr, it gives me an unrelated image.

System.Drawing.Bitmap RetrievedImage= new System.Drawing.Bitmap(1280, 1024, 8,

System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ImageData); //ImageData is the IntPtr

boxPic.Image = RetrievedImage; //Display the image in PictureBox

I think there is a problem in the casting. Please advise. Thank you.

Re: Visual C++ General Getting a Bitmap image from unsigned char*


Instead of assigning pointer to another pointer, you have to make a deep copy to make the pixel data available for the Bitmap object.

I'm not an expert in C++/CLI.
The following post in my blog may help you to understand concept which is written using