在pictureBox C#中绘制透明像素

时间:2014-03-18 14:34:52

标签: c# winforms image

我有一个图片框,并将.png图片导入。 现在当我旋转实际图像时,图片框的“自由”空间(不包括图像)被黑色像素填充。

问:如何让黑色像素透明?

修改

这是轮换代码

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;
        }

以下是原始图片和修改后的图片:

Original image

Rotated image with black pixels

1 个答案:

答案 0 :(得分:0)

如果将SupportsTransparentBackColor的{​​{1}}属性设置为true,则PictureBox支持透明度,将其放在Form_Load中。

System.Windows.Forms.ControlStyles

如果您愿意,可以将PictureBox上的this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true); 设置为其他颜色,默认情况下为BackColor

相关问题