I am very new to the matter of storing images in the database, and even when I thought it was very straightforward, it wasnβt. What I'm trying to do is read the image file from the same computer in any format, display it in the image window, and then convert the image to bytes to save it to the database. So far, I can display the image in the image window, but I cannot convert the image to bytes. Here is my code:
private void DisplayImage() { if (openFileDialog.ShowDialog(this) == DialogResult.OK) { try { Stream file; if ((archivo = openFileDialog.OpenFile()) != null) { using (file) { pictureBox.Image = Image.FromStream(file); } } } catch (Exception ex) { ... } } }
This is a simple method that simply displays an image in an image window. The real problem is the following method:
public static byte[] ConvertImageToBytes(Image image) { if (image != null) { MemoryStream ms = new MemoryStream(); using (ms) { image.Save(ms, ImageFormat.Bmp); byte[] bytes = ms.ToArray(); return bytes; } } else { return null; } }
When he tries to save the image in a memory stream, I get an error message:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Any ideas on what's going on?
source share