Good day all!!! I have a for with a picture box on it. I want to custom draw graphics to the image based on the checked radiobutton from a list of 15 radiobuttons. The graphic could either be a shape of text. I have managed to do this but now I'm stuck at how to implement a undo button for the graphics that are drawn to the image.
I have the following code which I used to just draw a rectange:
public interface ICommand
{
void Do();
void Undo();
}
public class SquareCommand : ICommand
{
private Point _point;
private Bitmap _bitmap;
private Graphics _graphics;
private Color _color;
public SquareCommand(Bitmap bitmap, Color color, int x, int y)
{
_bitmap = bitmap;
_graphics = Graphics.FromImage(_bitmap);
_color = color;
_point = new Point(x, y);
}
public void Do()
{
SaveCurrentPixels();
_graphics.DrawRectangle(new Pen(Color.Red, 2), new Rectangle(_point.X - (Width / 2), _point.Y - (Height / 2), Width, Height));
}
private const int Width = 8;
private const int Height = 8;
private IList<Color> _colors = new List<Color>();
private void SaveCurrentPixels()
{
for (var i = _point.X - (Width / 2) - 2; i < _point.X + (Width / 2) + 2; i++)
{
for (var j = _point.Y - (Height / 2) - 2; j < _point.Y + (Height / 2) + 2; j++)
{
_colors.Add(_bitmap.GetPixel(i, j));
}
}
}
public void Undo()
{
var ix = 0;
for (var i = _point.X - (Width / 2) - 2; i < _point.X + (Width / 2) + 2; i++)
{
for (var j = _point.Y - (Height / 2) - 2; j < _point.Y + (Height / 2) + 2; j++)
{
_bitmap.SetPixel(i, j, _colors[ix++]);
}
}
}
}
private void Dependant_Marker_MouseDown(object sender, MouseEventArgs e)
{
ICommand command = null;
command = new SquareCommand(_bitmap, Color.Red, e.X, e.Y);
command.Do();
_commandStack.Push(command);
Dependant_Marker.Refresh();
}
private void btnUndoMarker_Click(object sender, EventArgs e)
{
if (_commandStack.Count > 0)
{
var lastCommand = _commandStack.Pop();
lastCommand.Undo();
}
Dependant_Marker.Refresh();
}
private void btnClearMarker_Click(object sender, EventArgs e)
{
while (_commandStack.Count > 0)
{
_commandStack.Pop().Undo();
}
Dependant_Marker.Refresh();
}
But now I want it to work with shapes and text:
public void Do(RadioButton cmd)
{
SaveCurrentPixels();
switch (cmd.Text)
{
case "Filling":
_graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(_point.X - 11, _point.Y - 11, 22, 22));
break;
case "Unerupted":
_graphics.DrawLine(new Pen(Color.Blue, 3), _point.X - 2, _point.Y - 20, _point.X - 2, _point.Y + 20);
_graphics.DrawLine(new Pen(Color.Blue, 3), _point.X + 2, _point.Y - 20, _point.X + 2, _point.Y + 20);
break;
case "Extracted":
_graphics.DrawLine(new Pen(Color.Blue, 3), _point.X - 20, _point.Y - 20, _point.X + 20, _point.Y + 20);
_graphics.DrawLine(new Pen(Color.Blue, 3), _point.X + 20, _point.Y - 20, _point.X - 20, _point.Y + 20);
break;
case "Caries":
_graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(_point.X - 11, _point.Y - 11, 22, 22));
break;
case "To Extract":
_graphics.DrawLine(new Pen(Color.Red, 3), _point.X - 20, _point.Y - 20, _point.X + 20, _point.Y + 20);
break;
case "Ag.":
_graphics.DrawString("Ag.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "G.":
_graphics.DrawString("G.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "S.":
_graphics.DrawString("S.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "S.C.":
_graphics.DrawString("S.C.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "G.C.":
_graphics.DrawString("G.C.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "P.V.C":
_graphics.DrawString("P.V.C", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "R.C.":
_graphics.DrawString("R.C.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "A.C.":
_graphics.DrawString("A.C.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "P.":
_graphics.DrawString("P.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
case "M.":
_graphics.DrawString("M.", new Font("Arial", 11, FontStyle.Bold), new SolidBrush(Color.Green), _point);
break;
}
}
Thanx in advance...