views:

233

answers:

2

I'm using DrawImage() to paint 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

EDIT

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

+1  A: 

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.

Pete Kirkham
Thanks for your interest. Most of them are .ico and .png files. Here's a sample: http://icons.iconseeker.com/png/fullsize/cats-2/dot-mac-logo.png
CodeRush
With that png I don't see a halo using `e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);` as per the sample code in MSDN with 1:1 scaling. Windows Icons however traditionally had a one-bit mask, so would have the issue unless they are the newer format (since XP).
Pete Kirkham
Are you sure you are painting on a transparent background?
CodeRush
+1  A: 

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.

Mark Ransom