日期:2014-05-18  浏览次数:20763 次

把那块变成透明的
C# code

            Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(bit);

            g.FillRectangle(new SolidBrush(Color.Red), 0, 0, pictureBox1.Width, pictureBox1.Height);
            g.FillRectangle(new SolidBrush(Color.Transparent), 20, 20, 20, 20);//这块变透明

            pictureBox1.Image = bit;


预期效果是如何将其中那块变成透明的
现在出来的效果是一片红哈,

------解决方案--------------------
重绘制区域
C# code

            using (Graphics g = this.CreateGraphics()) {
                using (SolidBrush sb = new SolidBrush(Color.Red)) {
                    g.FillRectangle(sb, 10, 10, 200, 200);
                }
            }
            LPRECT rect = new LPRECT();
            rect.left = 20;
            rect.top = 20;
            rect.right = 100;
            rect.bottom = 100;
            InvalidateRect(this.Handle,out rect, true);

------解决方案--------------------
其实你完全可以用 Region 的 Exclude 来代替。
C# code

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    using (Bitmap bitmap = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        using (Region region = new Region(e.ClipRectangle))
        using (SolidBrush brush = new SolidBrush(Color.Red))
        {
            region.Exclude(new Rectangle(20, 20, 20, 20));
            g.FillRegion(brush, region);
        }

        e.Graphics.DrawImage(bitmap, new Point(0, 0));
    }
}

------解决方案--------------------
bitmap本身不支持透明。你所保存的东西,肯定不能是透明的。
------解决方案--------------------
Color.Transparent获取系统定义的颜色。这样做不能透明。 
Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
bit.MakeTransparent(Color.FromArgb(223, 7, 0));//(0,0,0)是RGB
Graphics g = Graphics.FromImage(bit);
//g.Clear(Color.Blue);
g.FillRectangle(new SolidBrush(Color.Red), 80, 0, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bit;
------解决方案--------------------
提供个思路 :


g.SetClip(Rectangle);
g.Clear(Color.Transparent);
------解决方案--------------------
起先new bitmap的时候 加一个PixelFormat.32Argb的格式

我想这样应该就没有问题了吧。