Clear Image

ricardotome

New member
Joined
Jul 6, 2005
Messages
1
Programming Experience
1-3
Hello,

I want to draw different types of drawings in a pictureBox image. I use:
One PictureBox
Two Graphics
Four Graphics States
I load an image and I atribute it to a pictureBox image. In the loading process I also save graphicsState. Then I have an event that checkes for two radio buttons. Depending on the check it will draw a violet or a turquoise circle on mouseDown event and also saves the graphicsState. My aim is to clear from the image the violet or turquoise circles. I have two buttons were I try to do that, but it simply don't work. Any one can help me? Here is the code:

private Graphics g1;
private Graphics g2;
private GraphicsState g1InicitalState;
private GraphicsState g2InicitalState;
private GraphicsState g1LastState;
private GraphicsState g2LastState;

private void LoadImage_Click(object sender, System.EventArgs e)
{
if(openFileDialog.ShowDialog() != DialogResult.Cancel)
{
pictureBox1.Image = Image.FromFile(openFileDialog.FileName);
g1 = Graphics.FromImage(pictureBox1.Image);
g2 = Graphics.FromImage(pictureBox1.Image);
g1InicitalState = g1.Save();
g2InicitalState = g2.Save();
}
}

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if(rdbDrawInG1.Checked)
{
g1.FillEllipse(new SolidBrush(Color.Violet),e.X,e.Y,10,10);
g1LastState = g1.Save();
pictureBox1.Invalidate();
}
else if(rdbDrawInG2.Checked)
{
g2.FillEllipse(new SolidBrush(Color.Turquoise),e.X,e.Y,10,10);
g2LastState = g2.Save();
pictureBox1.Invalidate();
}
}

private void clearG1_Click(object sender, System.EventArgs e)
{
g2.Restore(g2LastState);
g1.Restore(g1InicitalState);
pictureBox1.Invalidate();
}

private void clearG1_Click(object sender, System.EventArgs e)
{
g1.Restore(g1LastState);
g2.Restore(g2InicitalState);
pictureBox1.Invalidate();
}
 
You only need one graphics object to do the drawing.
To clear the image, simply reload the original image instead of restoring the graphics to a previous state.

A graphics object is the 'tool' by which you draw onto a source. That source can be a control (obtain the graphics object using a call to the CreateGraphics Function) or an image as you've shown.

A GraphicsState is used to save the state of a graphics object (not the source or image that has been drawn). It's usually used while performing some transformation to the graphics object (like rotating, scaling, clipping, ...)
 
Back
Top