Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save PictureBox.Image to file?

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));
like image 649
jacknad Avatar asked Jul 27 '11 14:07

jacknad


People also ask

How to save PictureBox image in c# windows application?

Solution 1. You can use the PictureBox class's ImageLocation property to load image from a file into the picture box and use PictureBox. Image. Save method to save the image of the PictureBox to a file.

How do you use PictureBox?

The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at a runtime. Specifies whether the picture box accepts data that a user drags on it. Gets or sets the path or the URL for the image displayed in the control.

Which method is used to load image in a PictureBox?

Load() Displays the image specified by the ImageLocation property of the PictureBox.


3 Answers

Try this

pictureBox.Image.Save(@"Path",ImageFormat.Jpeg);
like image 53
Donatas K. Avatar answered Nov 13 '22 10:11

Donatas K.


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);
like image 20
KV Prajapati Avatar answered Nov 13 '22 10:11

KV Prajapati


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);
                        }
                    }
like image 4
Mahadi Hassan Avatar answered Nov 13 '22 10:11

Mahadi Hassan