c# - Drawing a rectangle on a picture box -


when form opens, has background image of fullscreen , user can mouse down , draw rectangle used later crop background image. problem getting when form paints, it's not drawing rectangle user doesnt know the area selecting

this code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms;  namespace quick_screenshot {     public partial class crop : form     {         private rectangle croprect = rectangle.empty;         private bool mousedown = false;         private bool selectedarea = false;         point sp, ep;         bitmap background;          public rectangle croparea         {             { return croprect; }         }          public bool selectedarea         {             { return selectedarea; }         }          public crop(bitmap image)         {             initializecomponent();              background = image;             picbox.mousedown += new mouseeventhandler(crop_mousedown);             picbox.mouseup += new mouseeventhandler(crop_mouseup);             picbox.mousemove += new mouseeventhandler(crop_mousemove);             picbox.paint += new painteventhandler(crop_paint);          }          private void crop_load(object sender, eventargs e)         {             picbox.image = (image)background;         }          private void crop_mousedown(object sender, mouseeventargs e)         {             if (e.button == system.windows.forms.mousebuttons.right)                 this.close();             else             {                 mousedown = true;                 sp = ep = e.location;             }         }          private void crop_mouseup(object sender, mouseeventargs e)         {             ep = e.location;             mousedown = false;             croprect = getrectangle(sp, ep);              if (croprect.width > 10 && croprect.height > 10)             {                 selectedarea = true;             }             else             {                 croprect = rectangle.empty;                 taskbar.balloon("selected area small", "quick screenshot", tooltipicon.error);             }              this.close();         }          private void crop_mousemove(object sender, mouseeventargs e)         {             if (mousedown)             {                 ep = e.location;                 update();             }         }          private void crop_paint(object sender, painteventargs e)         {             e.graphics.drawrectangle(pens.red, getrectangle(sp, ep));             e.graphics.save();         }          private rectangle getrectangle(point p1, point p2)         {             return new rectangle(                 math.min(p1.x, p2.x),                 math.min(p1.y, p2.y),                 math.abs(p1.x - p2.x),                 math.abs(p1.y - p2.y)                 );         }     } } 

you should call invalidate() method on picbox variable refresh control.

instead of this:

private void crop_mousemove(object sender, mouseeventargs e) {     if (mousedown)     {         ep = e.location;         update();     } } 

use this:

private void crop_mousemove(object sender, mouseeventargs e) {     if (mousedown)     {         ep = e.location;         picbox.invalidate();     } } 

Comments