Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...