用鼠标画图和保存的问题

来源:互联网 发布:太极越狱 网络异常 编辑:程序博客网 时间:2024/06/06 09:53

Pen pen1 = new Pen(Color.White, 2);
        Pen pen2 = new Pen(Color.Black, 2);
        private Point p1, p2;
        Graphics gdraw;      
        public Form1()
        {
            InitializeComponent();
            Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            this.pictureBox1.Image = bmp;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                gdraw = Graphics.FromImage(this.pictureBox1.Image)  ; //this.pictureBox1.CreateGraphics();
                gdraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                p2 = e.Location;
                gdraw.DrawLine(pen2, p1, p2);                
                p1 = p2;
                this.pictureBox1.Refresh();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                p1 = p2;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                p1 = e.Location;
                p2 = e.Location;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Image.Save("C://001.bmp");
            Application.DoEvents();
            this.pictureBox2.ImageLocation = "C://001.bmp";
        }