asp.net:GDI中发生一般性错误+ [英] asp.net : A generic error occurred in GDI+

查看:146
本文介绍了asp.net:GDI中发生一般性错误+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个具有上传图片的Web服务一个asp.net 4.0 Web应用程序。我被从我的手机应用程序发送为Base64字符串形式的图像到Web服务上传图片。

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.

以下是我的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();
    }    

这code是我的本地ASP.NET开发服务器上工作很好,我能看到我的上传目录下上传的图片。然而,code传送到FTP目录之后,我现在得到以下错误:

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:

GDI +中发生一般性错误

我已签了上传目录有通过创建一个虚拟.aspx页面中,创造上的Page_Load一个文本文件适当的权限,并能正常工作。

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?

非常感谢在前进。

推荐答案

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

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

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

这篇关于asp.net:GDI中发生一般性错误+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆