如何在编辑后将图片框控件保存为jpeg文件

时间:2021-07-22 15:49:58

I have a PictureBox on my Windows Forms application.

我的Windows窗体应用程序上有一个PictureBox。

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

我在其中加载了一张图片,并在我的代码中启用了Paint事件。它绘制一个矩形。

Like this:

喜欢这个:

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);
}

And I click the "save" button:

然后我点击“保存”按钮:

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.

但是保存的文件永远不会包含我绘制的矩形。

Does anyone have any idea?

有人有什么主意吗?

5 个解决方案

#1


5  

You probably shouldn't draw directly on the PictureBox.

您可能不应该直接在PictureBox上绘图。

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

您需要使用位图。尝试将位图放在PictureBox.Image中,然后调用Save()。

Check this for more details

请查看此内容以获取更多详

#2


5  

Thanks.Your anwers all helped. This worked

谢谢。你们的帮助都很有帮助。这很有效

        private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation=@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000.jpg" ;
    }

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

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Graphics gr = Graphics.FromImage(bmp);
        Pen p = new Pen(Color.Red);
        p.Width = 5.0f;
        gr.DrawRectangle(p, 1, 2, 30, 40);
        pictureBox1.Image = bmp;
    }

#3


3  

Here is my solution with additional support to various file types:

这是我的解决方案,支持各种文件类型:

    public void ExportToBmp(string path)
    {
        using(var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
        {
        pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
        ImageFormat imageFormat = null;

        var extension = Path.GetExtension(path);
        switch (extension)
        {
            case ".bmp":
                imageFormat = ImageFormat.Bmp;
                break;
            case ".png":
                imageFormat = ImageFormat.Png;
                break;
            case ".jpeg":
            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                break;
            case ".gif":
                imageFormat = ImageFormat.Gif;
                break;
            default:
                throw new NotSupportedException("File extension is not supported");
        }

        bitmap.Save(path, imageFormat);
        }
    }

#4


3  

Here is a small example that clarified a few things for me (I was struggling with this a bit too).

这是一个小例子,为我澄清了一些事情(我也在努力解决这个问题)。

pBox is a PictureBox on Form1, make it at least 50x50

pBox是Form1上的PictureBox,使其至少为50x50

appPath was derived from System.Reflection but use any path you like

appPath派生自System.Reflection,但使用您喜欢的任何路径

There are two buttons, one for drawing, one for saving, their click events are in the code below.

有两个按钮,一个用于绘图,一个用于保存,它们的点击事件在下面的代码中。

Things I learned:

我学到的东西:

(1) "pBox.Image =" doesn't do anything but initialize the pBox image, it DOES NOT have to be a filename as EVERY example I found used (had problem saving to that same file because it was share locked). Also, if your goal is to see things on the entire control's surface, you'll probably like setting the size at initialize time to the size you need. I used the pBox's size in this example but normally I use the bitmap size (because I typically begin with a real picture file).

(1)“pBox.Image =”除了初始化pBox图像之外没有做任何事情,它不一定是文件名,因为我发现使用的每个例子(因为共享锁定而有问题保存到同一个文件)。此外,如果您的目标是在整个控件的表面上看到事物,您可能希望将初始化时的大小设置为您需要的大小。我在这个例子中使用了pBox的大小,但通常我使用的是位图大小(因为我通常以真实的图片文件开头)。

(2) I always had problems either seeing my draws show up on the control or seeing my changes saved in the output file (or both). In my prior attempts I would duplicate the draws both on the control and on the bitmap. Of course that isn't necessary but the edited bitmap DOES need to be reloaded into the control.image... and THAT was the piece of this puzzle I was missing.

(2)我总是遇到问题,要么看到我的绘图出现在控件上,要么看到我的更改保存在输出文件中(或两者都有)。在我之前的尝试中,我将在控件和位图上复制绘制。当然这不是必需的,但编辑后的位图需要重新加载到control.image中...而这就是我所缺少的这个难题。

(A) Create a bitmap from the control.image and draw on the bitmap

(A)从control.image创建一个位图并在位图上绘制

(B) Load the bitmap into the control.Image (so you can see the changes caused by the draw)

(B)将位图加载到control.Image中(这样你就可以看到绘制引起的变化)

(C) Save the control.Image

(C)保存control.Image

(2-option) You have a global (or passed) bitmap (probably from a real file)

(2选项)你有一个全局(或传递)位图(可能来自一个真实的文件)

(A) Draw on the bitmap

(A)绘制位图

(B) Load the bitmap into the control.Image (so you can see the changes)

(B)将位图加载到control.Image中(这样你就可以看到更改)

(C) Save the bitmap

(C)保存位图

    public Form1()
    {
        InitializeComponent();
        pBox.Image = new Bitmap(pBox.Width, pBox.Height);  
    }

    private void DrawStuff1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pBox.Image);  
        Graphics g = Graphics.FromImage(bmp);

        g.FillRectangle(Brushes.Red, 5, 5, 25, 25); //hard-coded size to reduce clutter

        pBox.Image = bmp;  //this makes your changes visible
    }

    private void Save_Click(object sender, EventArgs e)
    {
        pBox.Image.Save(appPath + "SavedImage.bmp");
    }

#5


1  

You need paint to image of picture, not to the Graphics control on Paint event.

您需要绘制图片的图像,而不是Paint事件上的Graphics控件。

EDIT:

编辑:

using( Graphics g = Graphics.FromImage( pictureBox1.Image ) ) {
    // there you will be do, what you do in Paint event
}

// ... somewhere else ...
pictureBox1.Save( _required_parameters_ );

#1


5  

You probably shouldn't draw directly on the PictureBox.

您可能不应该直接在PictureBox上绘图。

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

您需要使用位图。尝试将位图放在PictureBox.Image中,然后调用Save()。

Check this for more details

请查看此内容以获取更多详

#2


5  

Thanks.Your anwers all helped. This worked

谢谢。你们的帮助都很有帮助。这很有效

        private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation=@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000.jpg" ;
    }

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

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Graphics gr = Graphics.FromImage(bmp);
        Pen p = new Pen(Color.Red);
        p.Width = 5.0f;
        gr.DrawRectangle(p, 1, 2, 30, 40);
        pictureBox1.Image = bmp;
    }

#3


3  

Here is my solution with additional support to various file types:

这是我的解决方案,支持各种文件类型:

    public void ExportToBmp(string path)
    {
        using(var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
        {
        pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
        ImageFormat imageFormat = null;

        var extension = Path.GetExtension(path);
        switch (extension)
        {
            case ".bmp":
                imageFormat = ImageFormat.Bmp;
                break;
            case ".png":
                imageFormat = ImageFormat.Png;
                break;
            case ".jpeg":
            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                break;
            case ".gif":
                imageFormat = ImageFormat.Gif;
                break;
            default:
                throw new NotSupportedException("File extension is not supported");
        }

        bitmap.Save(path, imageFormat);
        }
    }

#4


3  

Here is a small example that clarified a few things for me (I was struggling with this a bit too).

这是一个小例子,为我澄清了一些事情(我也在努力解决这个问题)。

pBox is a PictureBox on Form1, make it at least 50x50

pBox是Form1上的PictureBox,使其至少为50x50

appPath was derived from System.Reflection but use any path you like

appPath派生自System.Reflection,但使用您喜欢的任何路径

There are two buttons, one for drawing, one for saving, their click events are in the code below.

有两个按钮,一个用于绘图,一个用于保存,它们的点击事件在下面的代码中。

Things I learned:

我学到的东西:

(1) "pBox.Image =" doesn't do anything but initialize the pBox image, it DOES NOT have to be a filename as EVERY example I found used (had problem saving to that same file because it was share locked). Also, if your goal is to see things on the entire control's surface, you'll probably like setting the size at initialize time to the size you need. I used the pBox's size in this example but normally I use the bitmap size (because I typically begin with a real picture file).

(1)“pBox.Image =”除了初始化pBox图像之外没有做任何事情,它不一定是文件名,因为我发现使用的每个例子(因为共享锁定而有问题保存到同一个文件)。此外,如果您的目标是在整个控件的表面上看到事物,您可能希望将初始化时的大小设置为您需要的大小。我在这个例子中使用了pBox的大小,但通常我使用的是位图大小(因为我通常以真实的图片文件开头)。

(2) I always had problems either seeing my draws show up on the control or seeing my changes saved in the output file (or both). In my prior attempts I would duplicate the draws both on the control and on the bitmap. Of course that isn't necessary but the edited bitmap DOES need to be reloaded into the control.image... and THAT was the piece of this puzzle I was missing.

(2)我总是遇到问题,要么看到我的绘图出现在控件上,要么看到我的更改保存在输出文件中(或两者都有)。在我之前的尝试中,我将在控件和位图上复制绘制。当然这不是必需的,但编辑后的位图需要重新加载到control.image中...而这就是我所缺少的这个难题。

(A) Create a bitmap from the control.image and draw on the bitmap

(A)从control.image创建一个位图并在位图上绘制

(B) Load the bitmap into the control.Image (so you can see the changes caused by the draw)

(B)将位图加载到control.Image中(这样你就可以看到绘制引起的变化)

(C) Save the control.Image

(C)保存control.Image

(2-option) You have a global (or passed) bitmap (probably from a real file)

(2选项)你有一个全局(或传递)位图(可能来自一个真实的文件)

(A) Draw on the bitmap

(A)绘制位图

(B) Load the bitmap into the control.Image (so you can see the changes)

(B)将位图加载到control.Image中(这样你就可以看到更改)

(C) Save the bitmap

(C)保存位图

    public Form1()
    {
        InitializeComponent();
        pBox.Image = new Bitmap(pBox.Width, pBox.Height);  
    }

    private void DrawStuff1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pBox.Image);  
        Graphics g = Graphics.FromImage(bmp);

        g.FillRectangle(Brushes.Red, 5, 5, 25, 25); //hard-coded size to reduce clutter

        pBox.Image = bmp;  //this makes your changes visible
    }

    private void Save_Click(object sender, EventArgs e)
    {
        pBox.Image.Save(appPath + "SavedImage.bmp");
    }

#5


1  

You need paint to image of picture, not to the Graphics control on Paint event.

您需要绘制图片的图像,而不是Paint事件上的Graphics控件。

EDIT:

编辑:

using( Graphics g = Graphics.FromImage( pictureBox1.Image ) ) {
    // there you will be do, what you do in Paint event
}

// ... somewhere else ...
pictureBox1.Save( _required_parameters_ );