new Bitmap(file) loosing Alpha component (NETCF)

G

Guest

I need to load Bitmaps and preserve the embedded Alpha channel information.
I tried the methods found here as posted by Alex / XrossGdi+ and also using
NETCF20.

Seemingly ALWAYS after (case1) the .GetObject P/Invoke or (case 2.0) new
Bitmap(filename) is invoked I am loosing the Alpha byte - it is set to FF.

Now, strangely (and unfortunately too slow in real app life) when I use
*target = src.GetPixel(x, y).ToArgb(); (target points to my buffer) I am
getting the correct alpha channel as it is contained in the binary bitmap
file.

Can anybody help here with either some magic to access the (obviously
present somewhere) real bitmap data or a howTo load Bitmaps under NETCF
without killing Alpha?

Thanks ahead!
tb
 
J

Jon Abaunza

I didn't understand exactly what you wanted to do with the Bitmap data.
Anyway if you want to work with the Bitmap data here is an example that
accesses the Bitmap data of two images in order to make alpha blending.
Hope if it will be helpful
public void ImageDissolving(Bitmap frontBitmap, int opacity, Bitmap
backBitmap, Rectangle rect)
{

double alpha = (double)opacity / 100;

BitmapData data1 = frontBitmap.LockBits(new Rectangle(0, 0,
frontBitmap.Width, frontBitmap.Height), ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
BitmapData data2 = backBitmap.LockBits(new Rectangle(0, 0,
backBitmap.Width, backBitmap.Height), ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);

unsafe
{
byte* ptr1 = (byte*)data1.Scan0 + (data1.Width *
rect.Top * 3) + rect.Left * 3;
byte* ptr2 = (byte*)data2.Scan0 + (data2.Width *
rect.Top * 3) + rect.Left * 3; ;
for (int i = 0; i < rect.Height &&
i+rect.Top<backBitmap.Height; i++)
{
for (int j = 0; j < rect.Width * 3 && (j / 3) +
rect.Left < backBitmap.Width; j++)
{
ptr2[0] = (byte)((1 - alpha) * ptr1[0] + alpha
* ptr2[0]);
ptr1++;
ptr2++;
}
ptr1 = (byte*)data1.Scan0 + (data1.Width *
(rect.Top + i) * 3) + rect.Left * 3;
ptr2 = (byte*)data2.Scan0 + (data2.Width *
(rect.Top + i) * 3) + rect.Left * 3;
}
}
frontBitmap.UnlockBits(data1);
backBitmap.UnlockBits(data2);
}
 
G

Guest

Jon, thanks for the post, yes, I try to work with Alpha channel data.
What your post said is how I am doing it in Step2. The trouble is in Step 1:
loading a Bitmap from file. (I am using "new Bitmap(filename)")

The bitmap I locate thereafter in memory (exactly like your code, only
format selected 32ARGB) actually conforms with 32ARGB but A, seemingly
depending on the filetype, is set to FF instead of the true Alpha, which is
contained in the bitmap file. (.BMP Alpha data is actually ok, .PNG not).

On a sidenote:
strange is that when I peek at the individual pixels (getpixel(x,y)), I am
actually getting the correct Alpha data that is defined in the PNG file.
Unfortunately that code is too slow for 'real life'.

So thats why I am asking: how do I convince the Bitmap loader to get me the
complete Alpha data (or use another loader is something's around?)

Cheers and Thank
Theo
 
G

Guest

Jon, thanks, that's clear. The funny thing though is that if I load a BMP
file that contains Alpha channel data (as described) that data is very well
delivered.

Only when loading PNGs the Alpha is replaced by FF (as documented for CE).

So it seems I need a separate loader once I am dealing with PNGs.

Once I have the data I am perfectly happy and have my own rendering working
on the bitmap.

Any suggestions for a c# (or COM) PNG loader / DLL ?

Thanks again
Theo
 

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