画一个低透明度的填充矩形。

时间:2023-02-09 17:45:47

I a have a PictureBox with a picture in a Windows Form application in C# language.I want draw a FillRectangle in some location of picturebox.but i also need to see picture of picture box.how can i draw this rectangle with low opacity to see image of picturebox?

我有一个图片框,在Windows窗体应用程序中使用c#语言。我想在图片框的某个位置画一个填充矩形。但是我还需要看图片框。如何用低不透明度绘制这个矩形来查看图片框的图像?

2 个解决方案

#1


54  

Do you mean:

你的意思是:

using (Graphics g = Graphics.FromImage(pb.Image))
{
    using(Brush brush = new SolidBrush(your_color))
    {
        g.FillRectangle(brush , x, y, width, height);
    }
}

or you can use

或者您也可以使用

Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))

where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.

从0到255,值为128就会有50%的可操作性。

#2


2  

You need to create a Graphics object based on your PictureBox image and draw what you want on it:

您需要基于您的图片框图像创建一个图形对象,并在其上绘制您想要的内容:

Graphics g = Graphics.FromImage(pictureBox1.Image);
g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 200, 200))
pictureBox1.Refresh()

Or as suggested by @Davide Parias you can use Paint event handler:

或者如@Davide Parias所建议的,您可以使用Paint事件处理程序:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 200, 200));
}

#1


54  

Do you mean:

你的意思是:

using (Graphics g = Graphics.FromImage(pb.Image))
{
    using(Brush brush = new SolidBrush(your_color))
    {
        g.FillRectangle(brush , x, y, width, height);
    }
}

or you can use

或者您也可以使用

Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))

where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.

从0到255,值为128就会有50%的可操作性。

#2


2  

You need to create a Graphics object based on your PictureBox image and draw what you want on it:

您需要基于您的图片框图像创建一个图形对象,并在其上绘制您想要的内容:

Graphics g = Graphics.FromImage(pictureBox1.Image);
g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 200, 200))
pictureBox1.Refresh()

Or as suggested by @Davide Parias you can use Paint event handler:

或者如@Davide Parias所建议的,您可以使用Paint事件处理程序:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 200, 200));
}