Click here to Skip to main content
12,079,489 members (60,522 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# drawing
I have a code for erase command:
  public void MyMouseDown(Object sender, MouseEventArgs e)
        {
Graphics graphics = CreateGraphics();
int linh = System.Int32.Parse(textBox3.Text);
 
graphics.FillRectangle(new SolidBrush(Color.White), e.X, e.Y, linh, linh);
 
SelectRect.Width = 0;
SelectRect.Height = 0;
SelectRect.X = e.X;
SelectRect.Y = e.Y;
 
SelectRect.Width = linh;
SelectRect.Height = linh;
}
 

public void MyMouseMove(Object sender, MouseEventArgs e)
        {
Graphics graphics = CreateGraphics();
int linh = System.Int32.Parse(textBox3.Text);
 
graphics.FillRectangle(new SolidBrush(Color.White), e.X, e.Y,linh,linh);
 
SelectRect.Width = 0;
SelectRect.Height = 0;
SelectRect.X = e.X;
SelectRect.Y = e.Y;
 
SelectRect.Width = linh;
SelectRect.Height = linh;
 
Form thisform = (Form)sender;
ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
}
Picture what this produces is here:

http://www63.zippyshare.com/v/17151299/file.html[^]

How to fix this, so the erase command would be just like in MS Paint? Only to show the rectangle for the eraser but not to draw it.
Posted 28-Nov-12 13:33pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You heavily misuse Graphics. In principle, in some cases it's possible to do what you are doing, but you cannot expect that such rendering will be shown permanently. You really should not create and instance of System.Windows.Graphics, you should handle System.Windows.Control.Paint event or override System.Windows.Control.OnPaint and the instance of Graphics passed in the event arguments.

I explained everything in my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100