如何保存PictureBox控件为JPEG文件,它的编辑后,它的、控件、编辑、文件

2023-09-03 00:27:50 作者:S亡家族回忆

我有一个图片框在我的Windows窗体应用程序。

I have a PictureBox on my Windows Forms application.

我加载在它的照片,我已经启用了油漆事件在我的code。它绘制一个矩形。

I load a picture in it and I have enabled the Paint event in my code. It draws a rectangle.

这样的:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics gr = e.Graphics;
    Pen p = new Pen(Color.Red);
    p.Width = 5.0f;
    gr.DrawRectangle(p, 1, 2, 30, 40);
}

和我点击保存按钮:

private void button2_Click(object sender, EventArgs e)
{
    pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000test.jpg",ImageFormat.Jpeg);
}

但保存的文件从来没有包含我画的矩形。

But the saved file never contains the rectangle that I drew.

没有人有任何想法?

推荐答案

您或许不应该直接对图片框绘制。

You probably shouldn't draw directly on the PictureBox.

您需要使用位图来代替。尝试把位图中PictureBox.Image,然后调用保存()。

You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save().

检查这个了解更多详细信息