0

I have a picture box, and import a .png picture into. Now when I rotate the actual image, the "free" space of the picture box, that does not include the image is filled by black pixels.

Q: How to make that black pixels transparent?

Edit:

Here is the rotation code

public static Bitmap RotateImage(Image image, PointF offset, float angle)
        {
            if (image == null)
            {
                return null;
            }
            try
            {
                Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
                rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                using (Graphics g = Graphics.FromImage(rotatedBmp))
                {
                    g.TranslateTransform(offset.X, offset.Y);
                    g.RotateTransform(angle);
                    g.TranslateTransform(-offset.X, -offset.Y);
                    g.DrawImage(image, new Point(0, 0));
                }
                return rotatedBmp;
            }
            catch (OutOfMemoryException)
            {
                GC.Collect();
            }
            catch (ArgumentException)
            {

            }
            return image as Bitmap;
        }

Here are original and modified images:

Original image

Rotated image with black pixels

Mike
  • 563
  • 5
  • 15
  • 32
  • Background color in the PictureBox instead? Or are you saying this "free" space is the image? – DonBoitnott Mar 18 '14 at 14:37
  • Like when you rotate the actual image in the picture box let's say 45 angle, that means that you have small triangles on opposite corners left free (without image). That triangles are rendered with black pixels. – Mike Mar 18 '14 at 14:37
  • If this seems to appear only after rotating, then you might want to show us that code in case it's that operation that is at fault. – DonBoitnott Mar 18 '14 at 14:39
  • 1
    `pictureBox1.BackgroundColor = Color.Transparent`? – DonBoitnott Mar 18 '14 at 14:40
  • Reference: http://stackoverflow.com/questions/5522337/c-sharp-picturebox-transparent-background-doesnt-seem-to-work – DonBoitnott Mar 18 '14 at 14:41
  • @DonBoitnott Yes, it is set – Mike Mar 18 '14 at 14:53
  • This question is *very* poorly documented, we can't see how the PNG specifies the transparency and whether the code that rotates the images ensures that no color shifts occur. Post a link to the image file and a snippet that shows code. – Hans Passant Mar 18 '14 at 15:32
  • What framework do u use and how do u import the image to the the picturebox ? – Sara S. Mar 19 '14 at 07:28
  • VS2012->DevExpress, I import it from DB. – Mike Mar 19 '14 at 07:30

1 Answers1

0

PictureBox supports transparency if you set the SupportsTransparentBackColor property of System.Windows.Forms.ControlStyles to true, put this in your Form_Load.

this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);

You can set BackColor on the PictureBox to another colour if you wish, by default it will be DefaultBackColor.

Dutts
  • 5,781
  • 3
  • 39
  • 61