2011-09-28 55 views
18

我有一个图片框在C#语言的Windows窗体应用程序中的图片。我想绘制一个FillRectangle在一些位置的picturebox.but我还需要看到图片box.how我是否可以绘制低透明度的此矩形以查看图片框的图像?绘制一个低透明度的填充矩形

+0

请参阅这里的问题和答案:http://stackoverflow.com/questions/1113437/drawing-colors-in-a-picturebox获取灵感来自这些答案,你基本上可以从那里复制粘贴:) –

回答

52

你的意思是:

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

,或者您可以使用

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

其中阿尔法从0到255,所以128的阿尔法值会给你50% opactity。

+0

像图形类型,刷子类型实现IDisposable接口。也许这个例子也应该证明这一点。 – tafa

+0

你需要考虑不是固体填充(低不透明度)的颜色阿尔法。 – hungryMind

2

您需要根据您的PictureBox图像上创建一个Graphics对象,并吸引你想要什么就可以了:

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

或者通过@Davide Parias的建议,你可以使用Paint事件处理程序:

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

在事件处理程序中:私人无效pictureBox_Paint(对象发件人,PaintEventArgs e)... –

相关问题