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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1063505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:20:39  来源:igfitidea点击:

How to save a picturebox control as a jpeg file after it's edited

c#.netpicturebox

提问by tguclu

I have a PictureBoxon my Windows Forms application.

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

I load a picture in it and I have enabled the Paintevent 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\WALKINGP0000test.jpg",ImageFormat.Jpeg);
}

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

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

Does anyone have any idea?

有谁有想法吗?

采纳答案by tzup

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 thisfor more details

检查这个以获取更多详细信息

回答by TcKs

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

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

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

回答by tguclu

Thanks.Your anwers all helped. This worked

谢谢。你的回答都有帮助。这有效

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

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKINGP0000test.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;
    }

回答by 0lukasz0

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

回答by shooky

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