Getting image as unsigned char array

A

arwen_h

Hi everyone,
I'm developing a form with .Net, in C++. I have to display a choosen
image on a pic-box and then I have to do some elaborations on it.
I need to get the Image (which I open from a dialog) as an unsigned char
array, some sort of:
image --> unsigned char buffer[]
because I have a whole of algorithms to use that use image-array like that.

I have already displayed the image and converted to Image class, but I dont
know
how to convert it into uchar array.

Thank you and forgive my poor english!
 
B

Bob Powell [MVP]

You could convert to a byte array fairly easily but the pixel format your
algorithms expect will be important.

You could use LockBits to get access to the raw image data but the stride of
the bitmap may be different to the width of the bitmap because of the
padding. You can then duplicate the image data in a byte array by scanning
through the image data.

Look at the explanation of LockBits in the GDI+ FAQ. That has info on how to
access the pixel data for all the different formats and bit depths.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

How about going the other way? I have an API that I'm calling that is
returning two items: a BITMAPINFO struct and a char* pointing to the raw
image bits. I need to get that translated into a System.Drawing.Bitmap
object. Here is what my code looks like:

The definition of the method from the .h file is:
===================================================
LIBFUNC int pdfConvertPageToBitmap(PDFViewerHandle viewer, int page, double
dpi, BITMAPINFOHEADER *dibHdr, char **bits);

Here's my C# code:
===================================================
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
public System.UInt32 biSize;
public System.Int32 biWidth;
public System.Int32 biHeight;
public System.UInt16 biPlanes;
public System.UInt16 biBitCount;
public System.UInt32 biCompression;
public System.UInt32 biSizeImage;
public System.Int32 biXPelsPerMeter;
public System.Int32 biYPelsPerMeter;
public System.UInt32 biClrUsed;
public System.UInt32 biClrImportant;
}
[DllImport("xpdfviewer.dll",EntryPoint="pdfConvertPageToBitmap")]
unsafe static extern void pdfConvertPageToBitmap(void* viewer, int page,
double dpi,
ref BITMAPINFOHEADER dibHdr, ref char* bits);

....

int page = 1;
int dpi = 72;
BITMAPINFOHEADER dibHdr = new BITMAPINFOHEADER();
char* bits = null;

pdfConvertPageToBitmap(viewer,page,dpi,ref dibHdr, ref bits);

I then tried the following to create a Bitmap object:
==================================================
Bitmap bmp = new
Bitmap(dibHdr.biWidth,dibHdr.biHeight,PixelFormat.Format24bppRgb);
BitmapData bd = bmp.LockBits(new
Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
bd.Scan0 = new IntPtr(&bits);
bmp.UnlockBits(bd);

The bitmap is the correct size, but is all black. Any ideas?

Thanks,
Todd Gray



Bob Powell said:
You could convert to a byte array fairly easily but the pixel format your
algorithms expect will be important.

You could use LockBits to get access to the raw image data but the stride of
the bitmap may be different to the width of the bitmap because of the
padding. You can then duplicate the image data in a byte array by scanning
through the image data.

Look at the explanation of LockBits in the GDI+ FAQ. That has info on how to
access the pixel data for all the different formats and bit depths.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





arwen_h said:
Hi everyone,
I'm developing a form with .Net, in C++. I have to display a choosen
image on a pic-box and then I have to do some elaborations on it.
I need to get the Image (which I open from a dialog) as an unsigned char
array, some sort of:
image --> unsigned char buffer[]
because I have a whole of algorithms to use that use image-array like
that.

I have already displayed the image and converted to Image class, but I
dont know
how to convert it into uchar array.

Thank you and forgive my poor english!

--
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
serena
aka
arwen_h
 

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