How to save PictureBox.Image to file?

88,427

Solution 1

Try this

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

Solution 2

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

Solution 3

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);
                        }
                    }
Share:
88,427
jacknad
Author by

jacknad

Electrical Engineer with experience in microprocessor hardware design, ASM, PL/M, C/C++, C#, Android, Linux, Python, and Java. First high school radio design burst into flames during the demo. First software program was FORTRAN on punch cards. Worked in FL, IL, ND, NJ, TX, VA, and WA.

Updated on July 09, 2022

Comments

  • jacknad
    jacknad almost 2 years

    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));
    
  • r.hamd
    r.hamd over 8 years
    i have used this PicBox.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp); there is picture in the picbox but the saved picture is full black
  • Kishan
    Kishan over 7 years
    What path is like pbImg.Image.Save(@"D:\Kishan\ki.jpg");