我的程序允许用户在图片框中绘制。我正在尝试将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;
}
}
谢谢!
您应该将graphics
存储在全局变量中并用于保存。
Graphics graphics = null;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(drawNote.isDraw)
{
if (graphics == null)
{
Bitmap bm = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
pictureBox1.Image = bm;
graphics = pictureBox1.CreateGraphics();
}
graphics.DrawLine(drawNote.pen, drawNote.X, drawNote.Y, e.X, e.Y);
graphics.Save();
drawNote.X = e.X;
drawNote.Y = e.Y;
}
}
您可以通过以下简单代码来完成此操作:
using (FileStream fileStream = new FileStream(@"C:\test.jpg", FileMode.Create))
{
pictureBox1.Save(fileStream, orginalImage.RawFormat);
}