Tuesday 15 June 2010

c# - How can i draw a rectangle around the pictureBox1 borders? -



c# - How can i draw a rectangle around the pictureBox1 borders? -

in picturebox1 paint event tried draw rectangle around image in picturebox1:

private void picturebox1_paint(object sender, painteventargs e) { e.graphics.drawrectangle(new pen(brushes.red, 5), new rectangle(0, 0, picturebox1.image.width, picturebox1.image.height)); }

but this:

and tried draw rectangle aorund picturebox1 self:

private void picturebox1_paint(object sender, painteventargs e) { e.graphics.drawrectangle(pens.green, 0, 0 , picturebox1.width, picturebox1.height); }

but in case i'm getting thick greenish line on left , top right , bottom without green.

the picturebox1 in desinger it's property sizemode set stretchimage how can draw rectangles in both cases ?

and how property of top line called ? it's not height maybe top ? if want find , draw on top of picturebox how called ?

to draw within picturebox easy:

private void picturebox1_paint(object sender, painteventargs e) { float penwidth = 5f; pen mypen = new pen (brushes.red, (int)penwidth); e.graphics.drawrectangle(mypen, penwidth / 2f, penwidth / 2f, (float)picturebox1.width - 2f * penwidth, (float)picturebox1.height - 2f * penwidth); mypen.dispose(); }

to draw outside picturebox need know command underneath it. eg if form utilize form paint:

private void form1_paint(object sender, painteventargs e) { int linewidth = 5; brush mybrush = new solidbrush (color.green); e.graphics.fillrectangle(mybrush, picturebox1.location.x - linewidth, picturebox1.location.y - linewidth, picturebox1.width + 2 * linewidth, picturebox1.height + 2 * linewidth); mybrush.dispose(); }

i using fillrectangle because part under picturebox not visible , easier command width.

c# .net winforms

No comments:

Post a Comment