How to call GDI CreateBitmap function using C#

G

Guest

Hi,

I do have a Bitmap object in a C# application, i would like to create a
bitmap using the GDI function CreateBitmap which could return me a valid
HBITMAP (Dont want to use the GetHBitmap method which returns a 24bits bitmap
which will loose any Alpha informations like transparency).

The declaration for the function will probably look like this :

[DllImport("gdi32.dll")]
public static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint
cPlanes, uint cBitsPerPel, short[] lpvBits);

But i have no clue how to provide the parameters CBitsPerPel and lpvBits,
and what they do correspond to in the .NET Bitmap object.

Any idea how i could call this function properly ?

Thx.
 
B

Bob Powell [MVP]

Parameters
nWidth
[in] Specifies the bitmap width, in pixels.
nHeight
[in] Specifies the bitmap height, in pixels.
cPlanes
[in] Specifies the number of color planes used by the device.
cBitsPerPel
[in] Specifies the number of bits required to identify the color of a
single pixel.
lpvBits
[in] Pointer to an array of color data used to set the colors in a
rectangle of pixels. Each scan line in the rectangle must be word aligned
(scan lines that are not word aligned must be padded with zeros). If this
parameter is NULL, the contents of the new bitmap is undefined.
You can set the cBitsPerPixel to 32 and leave the lpvBits as null.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

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

First thanks a lot for answering my question so quickly. But my question was
more about where are the data of the bitmap stored in the bitmap .NET object ?

For example, i instanciated a .NET bitmap object and i would like to call
the API CreateBitmap function to make a copy of the bitmap i got. I know
where to find the informations concerning the 4 first parameters (width,
height, number of planes, number of bits), but i have no clue where the data
of the bitmap is stored in the .NET Bitmap object.

Maybe u can help me with a sample implementation on how to retrieve these
data and to call the CreateBitmap function in order to get the similar
bitmap?

Thanks a lot. Regards.
 
B

Bob Powell [MVP]

To get access to the image data itself you need to use the LockBits method.

The article on my site explains LockBits in great detail.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

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.





Flo said:
First thanks a lot for answering my question so quickly. But my question
was
more about where are the data of the bitmap stored in the bitmap .NET
object ?

For example, i instanciated a .NET bitmap object and i would like to call
the API CreateBitmap function to make a copy of the bitmap i got. I know
where to find the informations concerning the 4 first parameters (width,
height, number of planes, number of bits), but i have no clue where the
data
of the bitmap is stored in the .NET Bitmap object.

Maybe u can help me with a sample implementation on how to retrieve these
data and to call the CreateBitmap function in order to get the similar
bitmap?

Thanks a lot. Regards.


Bob Powell said:
Parameters
nWidth
[in] Specifies the bitmap width, in pixels.
nHeight
[in] Specifies the bitmap height, in pixels.
cPlanes
[in] Specifies the number of color planes used by the device.
cBitsPerPel
[in] Specifies the number of bits required to identify the color of a
single pixel.
lpvBits
[in] Pointer to an array of color data used to set the colors in a
rectangle of pixels. Each scan line in the rectangle must be word aligned
(scan lines that are not word aligned must be padded with zeros). If this
parameter is NULL, the contents of the new bitmap is undefined.
You can set the cBitsPerPixel to 32 and leave the lpvBits as null.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

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.
 

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