0

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.

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
Andres Valencia
  • 483
  • 1
  • 6
  • 15
  • Are you sure that the path you're creating is valid? Also, you're not disposing of your `Image convertedImage`. – Dai May 04 '15 at 17:36
  • The path is correct, I tested it with another image strings and worked fine. Oh, I didn't know that I have to dispose it, thanks. @Dai – Andres Valencia May 04 '15 at 17:41
  • The user the web service is running under, does it have rights to write to `C:\Users\Andres\Pictures\ImagenesEvento\ `? That seems like a very odd folder for IIS to have rights to. – Scott Chamberlain May 04 '15 at 20:48
  • @ScottChamberlain yes it have permission to write – Andres Valencia May 05 '15 at 02:46

2 Answers2

0

The error occurs because the memory stream is closed. You can refer to this

public Image ConvertToImage(string image)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(image);
            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;

        }
Community
  • 1
  • 1
Chris
  • 599
  • 7
  • 23
0

I have found the solution to this problem, what it is only needed is to write all the image bytes using the class

File

This is my final code:

[WebMethod]
    public string SaveEventImage(string ImageByteArray, string ImageTitle)
    {
        try
        {
            byte[] bytes = Convert.FromBase64String(ImageByteArray);
            File.WriteAllBytes(EVTImagePath + ImageTitle + ".jpg",bytes);

            return "true";
        }
        catch
        {
            return "false";
        }
    }
Andres Valencia
  • 483
  • 1
  • 6
  • 15