双击PictureBox时图像会被删除

时间:2016-08-27 00:56:41

标签: c# winforms bitmap picturebox double-click

我写过这个Winforms应用程序。这是VS2013 solution in a zipped file

此应用程序仅用于在图片框中绘制一条具有以下条件的行

  • 按下鼠标左键只能绘制一条直线。
  • 如果绘制了第二行,则第一行将被删除,只有第二行将保留在图片框中。
  • 双击图片框会打开PictureBoxForm以显示图片框上绘制的线条。
  • 单击鼠标右键会弹出上下文菜单以保存绘制的线条。

此应用程序有一个问题,如果我双击图片框,绘制的线条会消失,然后再弹出PictureBoxForm。画布上只剩下一个圆圈。

如何解决此问题?

enter image description here

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!");
        }
    }
}

0 个答案:

没有答案
相关问题