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

发布于 2024-11-26 17:46:49 字数 555 浏览 5 评论 0原文

我使用以下命令将 jpgImage 写入 PictureBox.Image。

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

我可以使用以下命令将字节数组写入文件

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

,但如何从 PictureBox.Image 获取 jpgImage 字节数组,以便将其写入文件? IOW:如何反转以下内容以从 PictureBox.Image 获取字节数组?

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

I use the following to write jpgImage to a 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?
IOW: how do I reverse the following to get the byte array from the PictureBox.Image?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-12-03 17:46:49

试试这个

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

Try this

pictureBox.Image.Save(@"Path",ImageFormat.Jpeg);
最后的乘客 2024-12-03 17:46:49

您可以使用,

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

示例:

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

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);
記柔刀 2024-12-03 17:46:49

使用下面的代码保存到自定义位置

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

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);
                        }
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文