【问题标题】:asp.net : A generic error occurred in GDI+asp.net:GDI+ 中发生一般错误
【发布时间】:2011-12-03 15:59:27
【问题描述】:

我创建了一个 asp.net 4.0 Web 应用程序,它具有用于上传图像的 Web 服务。我通过将 Base64 字符串形式的图像从我的移动应用程序发送到 Web 服务来上传图像。

以下是我的代码:

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();
    }    

这段代码在我的本地 ASP.NET 开发服务器上运行良好,我能够在我的“上传”目录中看到上传的图像。但是,将代码传输到 FTP 目录后,我现在收到以下错误:

A generic error occurred in GDI+

我通过创建一个虚拟 .aspx 页面并在 page_load 上创建一个文本文件来检查上传目录是否具有适当的权限,并且它工作正常。

即使在谷歌搜索之后,我也无法解决这个问题。谁能帮我解决这个问题?

非常感谢。

【问题讨论】:

    标签: asp.net web-services base64 image-upload


    【解决方案1】:

    此外,我认为值得指出的是,当使用 MemoryStream 时,必须始终关闭流并且 save 方法必须在流关闭之前调用

     byte[] byteBuffer = Convert.FromBase64String(Base64String);
     MemoryStream memoryStream = new MemoryStream(byteBuffer);
     memoryStream.Position = 0;
     Bitmap bmpReturn = (Bitmap)Bitmap.FromStream(memoryStream);
     bmpReturn.Save(PicPath, ImageFormat.Jpeg);
     memoryStream.Close();
    

    【讨论】:

    • 在您的示例中,您需要添加一个 try/finally 以确保在发生异常时关闭流。无论如何,您应该始终为您的资源使用 using 语句!它们就是为此而生的。
    【解决方案2】:

    不要直接写入文件,而是将位图保存到MemoryStream,然后将流的内容保存到磁盘。这是一个古老的已知问题,坦率地说,我不记得为什么会这样的所有细节。

     MemoryStream mOutput = new MemoryStream();
     bmp.Save( mOutput, ImageFormat.Png );
     byte[] array = mOutput.ToArray();
    
     // do whatever you want with the byte[]
    

    在你的情况下,它可能是

    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 );
    }    
    

    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() );
        }
    }    
    

    【讨论】:

    • 嘿,感谢您的回复,我无法修改我的代码以包含您建议的更正。您能否通过修改我的代码来发布建议.. 提前致谢。
    • 谢谢,第一个选项对我有用。你救了我的一天!非常感谢... :-)
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    相关资源
    最近更新 更多