2011-09-28 56 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)... –

相關問題