views:

50

answers:

3

I need to create a image with a transparent background in .NETCF, I use magenta as the background I wish to make transparent. The way I have tried to do this is to override onPaint(). But I can't get the background transparent? Here's what I have:

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    ImageAttributes imageAttributs = new ImageAttributes();
    imageAttributs.SetColorKey(Color.FromArgb(255, 0, 255), 
        Color.FromArgb(255, 0, 255));
    g.DrawImage(cross, crossRect, 200, 10, cross.Width, cross.Height,
        GraphicsUnit.Pixel, imageAttributs);

    base.OnPaint(e);
}

But when I try to include the ImageAttributes my image is not drawn at all?

+1  A: 

The compact framework doesn't support transparency - you can achieve support it via COM interop though. Chris Lorton has a very good blog post on alpablending on the compact framework.

Rowland Shaw
A: 

It looks like OpenNETCF has managed wrappers around this already as well. I'm sure Chris Tacke could comment more about this. He seems to be pretty active on this site, but looks like beat him to this one :)

Bryan
+2  A: 

Ah, transparency in the CF. The hours and days one can (and did) waste on this. First, you might give us a little more info on the images you're using (bitmaps, png, etc) but we can probably deduce a little of it from your post. We also need to know if this is in a child container (like inside a frame, panel, etc).

Colorkey transparency is certainly supported (has been since 2.0 - maybe even earlier). The problem here is that you'll get parent "bleed through" if you're in a child. This appears to be what you're trying, but it's not completely obvious to me so I have a few follow up questions for clarification.

  • Is the OnPaint a Form override, or a custom control?
  • Why are you calling the base OnPaint() after your work (as opposed to before or not at all)?
  • Did you override OnPaintBackground?

My guess right now is that ypou have some bug in the way you're calling everything, but we don't have enough code to spot it.

Here are a few more resources on painting and transparency:

There are more resources for alpha-channel stuff (which is far from simple in the CF), but since it looks like you're attempting colorkey, these should be enough.

ctacke