保存绘制的图像图片框



我的程序允许用户绘制PictureBox

我正试图将pictureBox1保存为.jpg文件,但此文件为空。

我的保存按钮:

Bitmap bm = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
this.pictureBox1.DrawToBitmap(bm, this.pictureBox1.ClientRectangle);
bm.Save(String.Format("{0}.jpg", this.ID));
this.pictureBox1.CreateGraphics().Clear(Color.White);

我的抽奖活动:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
drawNote.isDraw = true;
drawNote.X = e.X;
drawNote.Y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(drawNote.isDraw)
{
Graphics G = pictureBox1.CreateGraphics();
G.DrawLine(drawNote.pen, drawNote.X, drawNote.Y, e.X, e.Y);
drawNote.X = e.X;
drawNote.Y = e.Y;
}
}

您应该通过该位图创建一个空的BimappictureBox1.Image,然后从中创建graphics,还必须将其存储在全局变量中以防止重新捕获。

像这样:

Graphics graphics = null;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(drawNote.isDraw)
{
if (graphics == null) 
{
graphics = pictureBox1.CreateGraphics();
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
pictureBox1.Image = bmp;
graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
}
graphics.DrawLine(drawNote.pen, drawNote.X, drawNote.Y, e.X, e.Y);
graphics.Flush();
graphics.Save();
pictureBox1.Refresh();
drawNote.X = e.X;
drawNote.Y = e.Y;
}
}

你可以通过这个简单的代码做到这一点:

using (FileStream fileStream = new FileStream(@"C:test.jpg", FileMode.Create))
{
pictureBox1.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

相关内容

  • 没有找到相关文章

最新更新