Hello, I'm trying to save image to SQL database but it gives me the error: A generic error occurred in GDI+. My purpose is that the user loads an image using OpenFileDialog and the image is set to an Image control. Then when the user clicks Update Profile, the image is saved to SQL field. When I try to save it from my PC, it works perfectly, but on the other PC it doesn't work.
First when the user chooses picture using OpenFileDialog, I set the path to a String variable, called imagePath. Then when trying to save the picture in database I use the following code:
command.CommandText = "update users set avatar = @image where id = '" + user_id + "'";
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
String str = Convert.ToBase64String(imageBytes);
ms.Close();
command.Parameters.AddWithValue("@image", str);
By the way when I catch the exception of the error, it points that there's an error with the function System.Drawing.Image.FromFile.
Anyway I checked the value of imagePath variable and I assured it was correct. As I said, it works on my PC, but not on the others. Do I need to send to others some file connected to GDI in order to work properly? Thanks in advance for any help!