开发者

C# compact framework : load a png alpha blending file

开发者 https://www.devze.com 2022-12-13 07:38 出处:网络
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.

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 = A开发者_运维百科ssembly.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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号