Transparent Background

This is a discussion on Transparent Background within the C# Programming forums, part of the General Programming Boards category; Exmple: We have an Image with "black ball on a white background" We set the image as a BackgroundImage on ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Transparent Background

    Exmple: We have an Image with "black ball on a white background"
    We set the image as a BackgroundImage on a panel

    I want the ball to be displayed but not the white background. Tried a lot of methods, but nothing seems to work.
    The MakeTransparent() method just makes the white color the background color of the main window.

    The closest thing I found is to achieve this and be able to overload the onPaint() handler the way I want, following instructions found somewhere.
    The problem is that I would need then a DrawImage() methods that excludes a specific Color, which I don't think it exists.

    My last effort would be to use GetPixel() and DrawPixel() and simply not draw the pixel with the specific color I want to exclude. But would like to do this automatically if possible.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    The problem is that I would need then a DrawImage() methods that excludes a specific Color, which I don't think it exists.
    Have you tried this?

    How to: Draw Images with Transparency

    It uses an overload of DrawImage() that takes an image attribute and you can use imageAttribute.SetColorKey() to set the transparent color key.
    Last edited by HLMetroid; 01-15-2010 at 04:09 PM.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yes, but I want for the color not to be painted at all. What this does is sets Red lets say to Transparent color, where the later is actually a color.

    All this stuff work perfectly fine on a single colored background, like the main window. But if you get one panel and place it on top of another, then the background of one panel overlaps the other. I would like for the background not to be painted at all.

    I have found a better example, which actually does what I want. It looks like

    Code:
    public class TPanel : Panel
        {
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                    return cp;
                }
            }
            protected void InvalidateEx()
            {
                if (Parent == null)
                    return;
                Rectangle rc = new Rectangle(this.Location, this.Size);
                Parent.Invalidate(rc, true);
            }
            protected override void OnPaintBackground(PaintEventArgs e)
            {
    
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                
            }
        }
    And what you do onPaint is transparent. Meaning that there is no background painted on the panel. And if I understant correctly, it fixes with InvalidateEx() the background of the object behind this panel. The parameter part I have no idea what it is.

    So, I would have to go pixel by pixel painting an image. Which might be too slow and troublesome. And I would guess that you should be able to do this automatically. Because overriding the OnPaint() method might not be a small task for a complicated situation.

    What I want is to have a chessboard for example and a panel with a picture of a pawn. But paint only the pawn, not its background.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    If I were doing this, I would not do a panel-over-a-panel approach at all. First, I'd use the PictureBox class, not the Panel class, as it logically makes more sense, and I'd use just one for the entire field of view, and combine the Bitmaps themselves as necessary. It's both overall simpler to code (though both ways have some pluses and some minuses) and more efficient in terms of the number of paint commands that need to be executed in order to repaint an area.

    You can easily have Bitmaps with transparent areas by loading images from a format (e.g. PNG) that supports an alpha channel, or you can use the DrawImage() technique to paint your sprites on your background.
    Last edited by Cat; 01-15-2010 at 07:45 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I cannot make DrawImage to work though. It draws the whole image. Whatever I do, it draws a rectange. And the OnPaint() happens only when one panel overlaps another. Otherwise the picture doesn't change. And in any case, my approach is simply not working.

    Alpha channel you say? I don't mind using PNG (I guess it is feasible to change all formats to that format). But what would I do from there?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transparent, Editbox, HOLLOW_BRUSH, Marking text?
    By Dampy in forum Windows Programming
    Replies: 6
    Last Post: 09-22-2008, 08:17 PM
  2. Transparent panels overlapped
    By AtomRiot in forum C# Programming
    Replies: 3
    Last Post: 04-21-2007, 10:16 PM
  3. Transparent background
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 11-02-2005, 08:02 PM
  4. Detect Close of a background process
    By Schwarzhelm in forum C Programming
    Replies: 1
    Last Post: 11-05-2003, 01:46 AM
  5. Transparent Bitmap - inserting into richeditctrl
    By LuckY in forum Windows Programming
    Replies: 1
    Last Post: 08-06-2003, 01:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21