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?
Asked
Active
Viewed 7.5k times
30
-
see question and answers here: http://stackoverflow.com/questions/1113437/drawing-colors-in-a-picturebox get inspired by those answers you can basically copy paste from there :) – Davide Piras Sep 28 '11 at 08:15
2 Answers
73
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.

Kirill Kobelev
- 10,252
- 6
- 30
- 51

Marco
- 56,740
- 14
- 129
- 152
-
Like Graphics type, Brush types implement the IDisposable interface. Maybe the example should demonstrate that fact, too. – tafa Sep 28 '11 at 08:21
-
You need to consider alpha of color for not solid fill (low opacity). – hungryMind Sep 28 '11 at 08:22
3
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:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 200, 200));
}
-
in the event handler: private void pictureBox_Paint(object sender, PaintEventArgs e)... – Davide Piras Sep 28 '11 at 08:16