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));
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.
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.
Load() Displays the image specified by the ImageLocation property of the PictureBox.
Try this
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With