开发者

How to eliminate "halo" in images with transparent backgrounds

I'm using DrawImage() to pai开发者_开发问答nt some images on a form with transparent background. Those images which are partly transparent cause a "halo" effect.(Look at the screenshot)

What's the best way to prevent that from happening

alt text http://www.imagechicken.com/uploads/1266798081003158300.png

EDIT

These same icons look great on my desktop so there should be some better way of painting the icons.


It's probably being caused by double rendering - the icon is rendering onto your transparent form, then the form is rendering over the background. Anyplace where your icon is only partially transparent, the form is losing all of its transparency.

Perhaps there is a change you can make to the form to support partial transparency rather than binary.


Is the source image full alpha or does it have a transparent colour/one bit alpha mask? It's common for one-bit alpha images to assume a background colour, often white and just fade to that. Your image looks exactly like what you get if you draw a one-bit alpha image on a non-white background.


If you use the one-bit transparency key, you get a 'halo', or rather mixing of the partially transparent values with the forms background color. So the 'halo' is pink in this form:

public  class Form1 : Form
{
    Image newImage;

    public Form1()
    {
        this.BackColor = Color.Pink;
        this.ClientSize = new Size(168, 168);
        this.TransparencyKey = this.BackColor;

        newImage = Image.FromFile(Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
               "dot-mac-logo.png"));
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(newImage, 20, 20);
    }
}

So don't use on-bit transparency with full alpha images - I'm not quite sure how to do it in C#, but in C you would use UpdateLayeredWindow MSDN one I made earlier to specify an alpha blend between an off-screen DC and the window surface.

There's a sample of using UpdateLayeredWindow from C# on this MSDN blog.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜