C# 将base64string转换为图像

C# 将base64string转换为图像,c#,C#,我正在将base64string从android应用程序发送到webservice。 webservice再次将base64string转换为图像。 但在将base64string再次转换为图像时,web服务出现以下错误: “GDI+中发生一般错误。”我正在服务器上获取此错误 只是 下面是我的代码: public string Base64ToImage(string base64String, string imgName) { try { // Convert

我正在将base64string从android应用程序发送到webservice。 webservice再次将base64string转换为图像。 但在将base64string再次转换为图像时,web服务出现以下错误:

“GDI+中发生一般错误。”我正在服务器上获取此错误 只是

下面是我的代码:

public string Base64ToImage(string base64String, string imgName)
{
    try
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);

        image.Save(
            "D:\\apps\\PJP_TEST\\PJPTest\\Online\\ImageStorage\\" + imgName);

        return "Success";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }           
}

我猜“服务器”是指“网络服务器”。您可能没有在文件夹
D:\apps\PJP\u TEST\PJPTest\Online\ImageStorage
中写入的权限

检查运行应用程序的用户是否具有对指定文件夹的读写权限

如上所述,直接写入文件更容易:

File.WriteAllBytes( Path.Combine
                    ( @"D:\apps\PJP_TEST\PJPTest\Online\ImageStorage\"
                    , imgName
                    )
                  , imageBytes
                  );

我猜“服务器”是指“网络服务器”。您可能没有在文件夹
D:\apps\PJP\u TEST\PJPTest\Online\ImageStorage
中写入的权限

检查运行应用程序的用户是否具有对指定文件夹的读写权限

如上所述,直接写入文件更容易:

File.WriteAllBytes( Path.Combine
                    ( @"D:\apps\PJP_TEST\PJPTest\Online\ImageStorage\"
                    , imgName
                    )
                  , imageBytes
                  );

为什么不直接将
imageBytes
的内容写入磁盘?为什么不将
imageBytes
的内容直接写入磁盘?如果是这种情况,则可能会出现IOException,而不是GDI+错误。@AVee:太糟糕了。确实如此。你说得对,它确实抛出了System.Runtime.InteropServices.ExternalException。是的,我认为这很糟糕(但这并不能使它不那么真实)。Avee:这是很模糊的,但是这是一个文件无法写入时所得到的错误信息。我猜在这个方法中有一些低级的Windows操作。这正是我刚才所做的:)如果是这样的话,你会期望出现IOException,而不是GDI+错误。@AVee:太糟糕了。确实如此。你说得对,它确实抛出了System.Runtime.InteropServices.ExternalException。是的,我认为这很糟糕(但这并不能使它不那么真实)。Avee:这是很模糊的,但是这是一个文件无法写入时所得到的错误信息。我猜在这种方法中有一些低级的Windows操作。这正是我刚才所做的:)