我写过这个Winforms应用程序。这是VS2013 solution in a zipped file。
此应用程序仅用于在图片框中绘制一条具有以下条件的行
PictureBoxForm
以显示图片框上绘制的线条。此应用程序有一个问题,如果我双击图片框,绘制的线条会消失,然后再弹出PictureBoxForm
。画布上只剩下一个圆圈。
如何解决此问题?
public partial class MainForm : Form
{
#region ctor
public MainForm()
{
InitializeComponent();
}
#endregion
#region Mouse Up and Down
Point _startPoint = Point.Empty;
private void left_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
_startPoint = e.Location;
Circle tempCircle = new Circle(_startPoint, 10);
Bitmap tempImage = new Bitmap(inputImagePictureBox.Width, inputImagePictureBox.Height);
Graphics g = Graphics.FromImage(tempImage);
tempCircle.Draw(g);
inputImagePictureBox.Image = tempImage;
}
}
private void pressed_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (_startPoint != e.Location)
{
Line tempLine = new Line(_startPoint, e.Location);
Bitmap tempImage = new Bitmap(inputImagePictureBox.Width, inputImagePictureBox.Height);
Graphics g = Graphics.FromImage(tempImage);
tempLine.Draw(g);
inputImagePictureBox.Image = tempImage;
}
}
}
Bitmap _savedImage;
private void left__MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (_startPoint != e.Location)
{
Line tempLine = new Line(_startPoint, e.Location);
Bitmap tempImage = new Bitmap(inputImagePictureBox.Width, inputImagePictureBox.Height);
Graphics g = Graphics.FromImage(tempImage);
tempLine.Draw(g);
_savedImage = tempImage;
inputImagePictureBox.Image = tempImage;
}
else
{
Bitmap tempImage = new Bitmap(inputImagePictureBox.Width, inputImagePictureBox.Height);
Graphics g = Graphics.FromImage(tempImage);
inputImagePictureBox.Image = tempImage;
}
}
}
#endregion
private void showPictureBoxForm(object sender, EventArgs e)
{
PictureBoxForm f = new PictureBoxForm(null, _savedImage);
f.ShowDialog();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (inputImagePictureBox.Image != null)
{
inputImagePictureBox.Image.Save(@"E:\___MSc in Computer Systems & Network\EMSC1,2,3\drawn.png");
}
else
{
MessageBox.Show("Nothing Saved!");
}
}
}