編集済み
パネルのコンテンツを描画します。これは、次のように Paint イベント ハンドラー内で行う必要があります。
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
パネルのコンテンツを画像として保存します。この部分は別の場所で行う必要があります (たとえば、[保存] ボタンをクリックしたとき)。
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(@"C:\testBitmap.jpeg", ImageFormat.Jpeg);
}
}