简体   繁体   English

如何将 PictureBox.Image 保存到文件?

[英]How to save PictureBox.Image to file?

I use the following to write jpgImage to a PictureBox.Image.我使用以下内容将 jpgImage 写入 PictureBox.Image。

var jpgImage = new Byte[jpgImageSize];
...
pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));

and I can use the following to write a byte array to a file我可以使用以下内容将字节数组写入文件

using (var bw =
    new BinaryWriter(File.Open(filename, FileMode.Create,
        FileAccess.Write, FileShare.None)))
{
    bw.Write(jpgImage);
}

but how can I get the jpgImage byte array from the PictureBox.Image so I can write it to the file?但是如何从 PictureBox.Image 获取 jpgImage 字节数组,以便将其写入文件? IOW: how do I reverse the following to get the byte array from the PictureBox.Image? IOW:如何反转以下内容以从 PictureBox.Image 中获取字节数组?

pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));

试试这个

pictureBox.Image.Save(@"Path",ImageFormat.Jpeg);

You may use,你可以使用,

pictureBox.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

Example:例子:

 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 byte[] ar = new byte[ms.Length];
 ms.Write(ar, 0, ar.Length);

Use below code for save into custom location使用以下代码保存到自定义位置

using (SaveFileDialog saveFileDialog = new SaveFileDialog() {Filter = @"PNG|*.png"})
                    {
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            pictureBox.Image.Save(saveFileDialog.FileName);
                        }
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM