Click here to Skip to main content
Rate this: bad
good
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 12: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)



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