Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...