I am working in an application that saves images via web service in asp.net but it throws an exception with the following message:
Generic Error in GDI+
These are the methods:
public class GuardaImagenes : System.Web.Services.WebService
{
[WebMethod]
public string GuardarImagen(string image,string imageTitle,string ext)
{
string ImageFolderPath = @"C:\Users\Andres\Pictures\ImagenesEvento\";
Image convertedImage = ConvertToImage(image);
try {
convertedImage.Save(ImageFolderPath+imageTitle+ext,ImageFormat.Jpeg);
return "true";
}catch(Exception ex){
return "false";
}
}
public Image ConvertToImage(string image) {
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(image);
using (var ms = new MemoryStream(imageBytes, 0,imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image img = Image.FromStream(ms, true);
return img;
}
}
}
I pass the image in Base64 string and use the ConvertToImage
method to save the image in the specified folder in my computer.
The problem is, when it comes when I want to save the image, the program crashes and can't handle it.
I've read that it's better to use MemoryStream
, but I can't implement it correctly.