Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

web services - asp.net : A generic error occurred in GDI+

I create an asp.net 4.0 web application which has a web service for uploading images. I am uploading images by sending the image in form of Base64 string from my mobile app to the web service.

Following is my code:

public string Authenticate(string username, string password, string fileID, string imageData)
    {
        Dictionary<string, string> responseDictionary = new Dictionary<string, string>();
        bool isAuthenticated = true; // Set this value based on the authentication logic

        try
        {
            if (isAuthenticated)
            {
                UploadImage(imageData);

                string result = "success";
                var message = "Login successful";

                responseDictionary["status"] = result;
                responseDictionary["message"] = message;
            }
        }
        catch (Exception ex)
        {

            responseDictionary["status"] = ex.Message;
            responseDictionary["message"] = ex.StackTrace;
        }

        return new JavaScriptSerializer().Serialize(responseDictionary);
    }

    private void UploadImage(string uploadedImage)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(uploadedImage);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Image.FromStream(ms);

        string uploadPath = Server.MapPath("..\uploads") + DateTime.Now.Ticks.ToString() + ".jpeg";
        ms.Close();

        bitmap.Save(uploadPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        bitmap.Dispose();
    }    

This code was working fine on my local ASP.NET development server and I was able to see the uploaded image in my "uploads" directory. However, after transferring the code to the FTP directory, I am now getting the following error:

A generic error occurred in GDI+

I have checked that the upload directory has proper permission by creating a dummy .aspx page and creating a text file on page_load, and it works fine.

Even after doing google search, I was not able to solve this problem. Can anybody help me fixing this?

Thanks a lot in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of writing directly to files, save your bitmap to a MemoryStream and then save the contents of the stream to disk. This is an old, known issue and, frankly, I don't remember all the details why this is so.

 MemoryStream mOutput = new MemoryStream();
 bmp.Save( mOutput, ImageFormat.Png );
 byte[] array = mOutput.ToArray();

 // do whatever you want with the byte[]

In your case it could be either

private void UploadImage(string uploadedImage)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(uploadedImage);

    string uploadPath = Server.MapPath("..\uploads") + DateTime.Now.Ticks.ToString() + ".jpeg";

    // store the byte[] directly, without converting to Bitmap first 
    using ( FileStream fs = File.Create( uploadPath ) )
    using ( BinaryWriter bw = new BinaryWriter( fs ) )
       bw.Write( imageBytes );
}    

or

private void UploadImage(string uploadedImage)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(uploadedImage);
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

    System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Image.FromStream(ms);

    string uploadPath = Server.MapPath("..\uploads") + DateTime.Now.Ticks.ToString() + ".jpeg";
    ms.Close();

    // convert to image first and store it to disk
    using ( MemoryStream mOutput = new MemoryStream() )
    {  
        bitmap.Save( mOutput, System.Drawing.Imaging.ImageFormat.Jpeg);
        using ( FileStream fs = File.Create( uploadPath ) )
        using ( BinaryWriter bw = new BinaryWriter( fs ) )
            bw.Write( mOutput.ToArray() );
    }
}    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...