Well, you may have overlooked a point or two, so I'm repeating the
procedure.
All the drawing you are doing, do it directly on the Bitmap.
Whenever you need to refresh the screen or display the result of the
drawing, paste the Bitmap onto the PictureBox, using
Graphics.DrawImageUnscaled(Bitmap, x, y)
When you need to save the drawing to file, you just call Image.Save on the
Bitmap you use for drawing.
Bitmap bm;
public TestForm()
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// If you aren't doing any drawing here you might just paste the Bitmap
// Graphics g = Graphics.FromImage(bm);
// g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
// g.Dispose();
e.Graphics.DrawImageUnscaled(bm, 0, 0);
}
private void button1_Click(object sender, EventArgs e)
{
bm.Save("test.jpg", ImageFormat.Jpeg);
}
private void DrawSomeStuff()
{
using(Graphics g = Graphics.FromImage(bm))
{
g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
}
using(Graphics h = pictureBox1.CreateGraphics())
{
h.DrawImageUnscaled(bm, 0, 0);
}
}
(PS! code may contain typos as it is not tested)