2

I have a picturebox with a png in it. Yet even when I set the BackColor to Transparent, it is not transparent. Any ideas what might be wrong? :)

alt text

Thanks!

Rob
  • 719
  • 5
  • 11
  • 16
  • The screen shot is of the designer - does this also happen when running the application? – Oded Dec 11 '10 at 13:32
  • It looks like there's a gradient in that image. Even if you find the right color for the alpha channel, that gradient will be ugly as sin. – kenny Dec 11 '10 at 13:34
  • The PNG does have a transparent background. If I set other colors as BackColor, the color is seen. Therefor the png itself if ok. – Rob Dec 11 '10 at 13:45
  • Its a shadow, not a gradient. Looks fine in photoshop. :) – Rob Dec 11 '10 at 13:45
  • I think a picture box doesn't support transparent background at all. – CodesInChaos Dec 11 '10 at 14:24
  • 2
    It is *not* transparent, it has a complete gradient to the edge. From dark-gray to light-gray. Zoom in to see that. It can only look good on a background that's the same shade of light-gray. Anti-aliased images don't work with transparency at all. – Hans Passant Dec 11 '10 at 14:32
  • 2
    it is transparent, the light gray color is coming from the form background. – Rob Dec 11 '10 at 14:55

5 Answers5

6

I have also faced the problem regarding transparent pictures.

you have to Draw it through code. See my question A PictureBox Problem

EDIT:

In paint event (Control containing Background Image) write this

  //If added your image in project's resources get from there OR your Image location
  Image img = yourNamespace.Properties.Resources.yourPicture;   
  e.Graphics.DrawImage(img,50,50,100,100); 
Community
  • 1
  • 1
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
3

Your PNG file should also have the transparent background. This is can be done while creating the image(png) files.

Hps
  • 1,177
  • 8
  • 9
  • The PNG does have a transparent background. If I set other colors as BackColor, the color is seen. Therefor the png itself if ok. – Rob Dec 11 '10 at 13:44
  • I did some searching on it and looks like other guys also faced this problem. Someone suggested using a Panel control instead of PictureBox. Is that an option for you? – Hps Dec 11 '10 at 14:08
2

You really have to draw it through code. Place a pictureBox on your form, set sizeMode and docking as you like. Then you may fire the following function on the pictureBox's PAINT event:

    public void LogoDrawTransparent(PaintEventArgs e)
    {
        // Create a Bitmap object from an image file.
        Image myImg;
        Bitmap myBitmap;
        try
        {
            myImg = cls_convertImagesByte.GetImageFromByte(newImg);
            myBitmap = new Bitmap(myImg); // @"C:\Temp\imgSwacaa.jpg");  

            // Get the color of a background pixel.
            Color backColor = myBitmap.GetPixel(0, 0); // GetPixel(1, 1); 
            Color backColorGray = Color.Gray;
            Color backColorGrayLight = Color.LightGray;
            Color backColorWhiteSmoke = Color.WhiteSmoke;
            Color backColorWhite = Color.White;
            Color backColorWheat = Color.Wheat;

            // Make backColor transparent for myBitmap.
            myBitmap.MakeTransparent(backColor);
                    // OPTIONALLY, you may make any other "suspicious" back color transparent (usually gray, light gray or whitesmoke)
            myBitmap.MakeTransparent(backColorGray);
            myBitmap.MakeTransparent(backColorGrayLight);
            myBitmap.MakeTransparent(backColorWhiteSmoke);

            // Draw myBitmap to the screen.
            e.Graphics.DrawImage(myBitmap, 0, 0, pictureBox1.Width, pictureBox1.Height); //myBitmap.Width, myBitmap.Height);
        }
        catch
        {
            try { pictureBox1.Image = cls_convertImagesByte.GetImageFromByte(newImg); }
            catch { } //must do something
        }
    }

This is my class that is referenced in the above function:

    class cls_convertImagesByte
{

    public static Image GetImageFromByte(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }

    public static byte[] GetByteArrayFromImage(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }
}

Thanks. chagbert.

Chagbert
  • 722
  • 7
  • 16
0

From what I learned I can't do it within a windows form, as it doesn't have layers for the images. So guess will have to make it as WPF. :)

Rob
  • 719
  • 5
  • 11
  • 16
0

How did you create the background? Is that set by setting the Form.BackgroundImage? If that background (the paper like image) is a container control, the transparency should just work. However, If you are placing two PictureBox objects on top of eachother this doesn't work. The transparent area takes on the color of its parent object. If you have two PictureBox objects they both will have the Form as their parent. If this is your situation, it can be solved by setting the transparent image's Parent property to be the background image.

    private void Form1_Load(object sender, EventArgs e)
    {
        transparentPictureBox.Parent = backgroundPictureBox;
    }

When changing the Parent property, the Location of the tranparentPictureBox will become relative to its new parent. You'd have to subtract the x and y coordinates of the background image from the transparent image. See my answer on A PictureBox Question for an example with a screen shot.

AFAIK, you can not set the Parent in the Designer, only in code. Therefore, the Designer will still not show a transparent image, but at runtime it should.

The same problem occurs if you place a transparent Label on top of a PictureBox object.

Community
  • 1
  • 1
comecme
  • 6,086
  • 10
  • 39
  • 67