在一个picturebox中,用这个函数在鼠标移动里面画图,但是保存的时候保存的是加载的图片,没有把画的部分写入到图片里面
画图部分代码
g = pictureBox1.CreateGraphics();
//Graphics g = Graphics.FromImage(bmp);
if (e.Button == MouseButtons.Left)
{
if (drawing)
{
Point currentPoint = new Point(e.X, e.Y);
//画图正常
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
Region region12 = new Region(new Rectangle(pt2.X, pt2.Y, Convert.ToInt32(comboBox1.Text.ToString()), Convert.ToInt32(comboBox1.Text.ToString())));
g.FillRegion(new SolidBrush(Color.Blue), region12);
//能保存
//pictureBox1.Image = DrawLineOnPicture(pictureBox1.Image, Color.Blue, 3, pt1.X, pt1.Y, pt2.X, pt2.Y);
//
//g.DrawImage(pictureBox1.Image,pictureBox1.Width,pictureBox1.Height);
pt2.X = currentPoint.X;
pt2.Y = currentPoint.Y;
currentPoint2 = currentPoint;
}
}
if (e.Button == MouseButtons.Right)
{
if (drawing)
{
Point currentPoint = new Point(e.X, e.Y);
Region region1 = new Region(new Rectangle(pt2.X, pt2.Y, Convert.ToInt32(comboBox1.Text.ToString()), Convert.ToInt32(comboBox1.Text.ToString())));
pt2.X = currentPoint.X;
pt2.Y = currentPoint.Y;
pictureBox1.Invalidate(region1);
//pictureBox1.Refresh();
}
}
保存代码
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"C:\Users\Administrator\Desktop\视觉\11.bmp", System.Drawing.Imaging.ImageFormat.Bmp);