I am taking some (jpeg) images with a camera and inserting them to the database (as a blob). In order to insert them to the database, I have to pass the image in a byte array.
Here is a little code that does the conversion to the byte array:
public static byte[] JpegToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //PROBLEM IS HERE!
return ms.ToArray();
}
Although, I am sure that the image I pass is in jpeg format, "imageIn.Save(...)" throws an error as follows:
Here is the definition of saving method:
public void Save(Stream stream, ImageFormat format);
//
// Summary:
// Saves this System.Drawing.Image to the specified file in the specified format.
//
// Parameters:
// filename:
// A string that contains the name of the file to which to save this System.Drawing.Image.
//
// format:
// The System.Drawing.Imaging.ImageFormat for this System.Drawing.Image.
//
// Exceptions:
// System.ArgumentNullException:
// filename or format is null.
//
// System.Runtime.InteropServices.ExternalException:
// The image was saved with the wrong image format.-or- The image was saved
// to the same file it was created from.
The image(s) I retrieve and pass to the function are never stored to/read from the file system. After I insert the image to the database, I dispose it. And the method that I retrieve images just returns a list of System.Drawing.Image (with 4 elements). There is nothing special about it.
Do you guys have any idea why this might happen?
Thanks for your time.
EDIT:
I am able to set the images to the picturebox, for example:
pictureBox1.Image = imageIn;
picturebox.Refresh();
But, cannot save the images neither to MemoryStream, nor to the file system.