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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With