views:

1477

answers:

2

I have a png image file with alpha blending of its own. Now I want to load it onto a form in a mobile. I tried so many ways but not work. Is there any solution? Thanks in advance. This is what I use to load the image from resource:

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("drawObj.Graph.png");
Bitmap myPNGImg = new Bitmap(stream);

Then create new bitmap with same size of the images Graph.png:

Bitmap myBlankImg = new Bitmap(48,48);

Graphics  mynewGraph = Graphics.FromImage(myNew);

mynewGraph.Clear(Color.Transparent);

Draw the PNG bitmap: mynewGraph.DrawImage(myPNGImg, 0, 0); And then something I read from internet:(

Rectangle rectDest = new Rectangle(50,50, 100, 100);

ImageAttributes imgatt = new ImageAttributes();

imgatt.SetColorKey(Color.Transparent, Color.Transparent);

myGraph.DrawImage(myNew, rectDest, 0, 0, 99, 99,GraphicsUnit.Pixel, imgatt);

It works, but just clear the four corner of the images(somekind of rounded rectangle). There's still some white border around the images left.

+1  A: 

Loading images using Bitmap on Compact Framework will lose alpha information. Setting the color key is an alternative way of doing transparency where you sacrifice an exact single color as the transparent color.

To use alpha blending on Compact Framework, you can use the helper classes from OpenNETCF to load the PNG file, keeping the alpha information (see Transparency and alpha blending), then P/Invoke AlphaBlend. It's not pretty, but it's what it takes. Also be warned that you will take a heavy performance hit for using alpha blending. To bake some images dynamically, it's fine, but for generic on screen drawing operations, you might want to use another approach.

Freed
Im not trying to make transparent all my png image. I just need to clear all the transparence in my png image and draw the left image on to the form. So do I have to do the step P/Invoke AlphaBlend as you told?
Thyphuong
As someone pointed out (and then deleted their comment?), that issue likely comes from an antialiased image, where parts of the image is not fully transparent, nor fully opaque. The method you're using only handles the cases there there is an single exact color to use for representing transparent parts of the image. If that's all you need, you can get away with just fixing up the image first using your favourite image manipulation program.
Freed
Problem here is that Im not allowed to edit the images and I can't import the OpenNetCF either. Any other way? Thanks
Thyphuong
Do whatever OpenNetCF does to load the images (source is available) and flatten it once when you load the image (or use AlphaBlend). If you know the background the image is to be painted on, the most efficient way is probably to AlphaBlend once into a new bitmap, then paint that without any transparency.
Freed
A: 

The problem is that in the CF, filling with Color.Transparent actually fills with white (see these two blog entries). Project Resistance has a very good example of how to do this blending (actually several of them).

ctacke