I have a C# DLL that loads a bitmap object from a PNG file. Thanks to Bob
Powell's advice i managed to access the Bitmap data using the lockbits method
and then call the Windows API function CreateBitmap successfully. Then the
DLL provides a COM interface which returns the handle to the bitmap created
using CreateBitmap function.

The problem is that the initial bitmap has a transparent background color,
and when using the CreateBitmap function, the bitmap seems to loose this
color, which is then replaced by black (maybe something to see with
ColorPalette). Furthermore, the createdbitmap is a 32bit bitmap so the alpha
layer which contains transparency informations should be the same. So i don't
know what i missing or doing wrong. here is the code that creates the bitmap
:

Bitmap bitmap = (Bitmap) Bitmap.FromFile(@"C:\tmpicon.png");

BitmapData data = bitmap.LockBits(new Rectangle(new Point(0,0),
bitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

IntPtr HBITMAP = CreateBitmap(bitmap.Width, bitmap.Height, 1, 32, data.Scan0);

NB : I also noticed that when opening the PNG file with MS Paint, the
transparent color is replaced by black, whereas opening it with Photoshop or
Windows Picture viewer, the transparency is visible. Can't explain this...

I'd like to understand what im missing to keep the transparent color on the
created bitmap.

Thx for any help.

Re: Problem using API CreateBitmap function and keeping transparent co by Michael

Michael
Thu Sep 15 18:39:33 CDT 2005

CreateBitmap produces a bitmap with the colors formatted as BGR.
The alpha channel is ignored.

You want a bitmap with the colors formatted as ARGB.

You have two choices.

1) Use CreateDIBitmap to produce a Device Dependent Bitmap.
2) Use CreateDIBSection to produce a Device Independent Bitmap.

In both cases, you must populate a BITMAPV5HEADER with the color
masks for an alpha channel and create your bitmap with the proper format
(i.e., ARGB).

Once you have created the alpha channel formatted bitmap,
You may transfer the bits with BitBlt or transfer them scanline
by scanline. You simply copy a scanline of DWORD's formatted
as ARGB.


"Flo" <Flo@discussions.microsoft.com> wrote in message
news:498BC9D8-8D0F-4733-AD9A-F2230FE41E2E@microsoft.com...
>I have a C# DLL that loads a bitmap object from a PNG file. Thanks to Bob
> Powell's advice i managed to access the Bitmap data using the lockbits
> method
> and then call the Windows API function CreateBitmap successfully. Then the
> DLL provides a COM interface which returns the handle to the bitmap
> created
> using CreateBitmap function.
>
> The problem is that the initial bitmap has a transparent background color,
> and when using the CreateBitmap function, the bitmap seems to loose this
> color, which is then replaced by black (maybe something to see with
> ColorPalette). Furthermore, the createdbitmap is a 32bit bitmap so the
> alpha
> layer which contains transparency informations should be the same. So i
> don't
> know what i missing or doing wrong. here is the code that creates the
> bitmap
> :
>
> Bitmap bitmap = (Bitmap) Bitmap.FromFile(@"C:\tmpicon.png");
>
> BitmapData data = bitmap.LockBits(new Rectangle(new Point(0,0),
> bitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
>
> IntPtr HBITMAP = CreateBitmap(bitmap.Width, bitmap.Height, 1, 32,
> data.Scan0);
>
> NB : I also noticed that when opening the PNG file with MS Paint, the
> transparent color is replaced by black, whereas opening it with Photoshop
> or
> Windows Picture viewer, the transparency is visible. Can't explain this...
>
> I'd like to understand what im missing to keep the transparent color on
> the
> created bitmap.
>
> Thx for any help.