Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency in compact framework

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?

like image 386
DNRN Avatar asked Sep 07 '10 14:09

DNRN


2 Answers

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:

  • How To: draw images with transparency
  • Transparent buttons, panels and controls
  • Keeping Bitmap transparency when copying
  • Bitmap.Clear bug

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.

like image 91
ctacke Avatar answered Sep 27 '22 22:09

ctacke


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.

like image 37
Rowland Shaw Avatar answered Sep 27 '22 23:09

Rowland Shaw