“GDI +中发生了一般性错误”显示上传的图像时出错 [英] "A generic error occurred in GDI+" error while showing uploaded images

查看:171
本文介绍了“GDI +中发生了一般性错误”显示上传的图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从我的asp.net mvc(C#)应用程序中显示已保存在我的数据库中的图像:。

i am using the following code to show the image that has been saved in my database from my asp.net mvc(C#) application:.

    public ActionResult GetSiteHeaderLogo()
        {
            SiteHeader _siteHeader = new SiteHeader();
            Image imgImage = null;

            long userId = Utility.GetUserIdFromSession();

            if (userId > 0)
            {
                _siteHeader = this.siteBLL.GetSiteHeaderLogo(userId);

                if (_siteHeader.Logo != null && _siteHeader.Logo.Length > 0)
                {
                    byte[] _imageBytes = _siteHeader.Logo;
                    if (_imageBytes != null)
                    {
                        using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
                        {
                             imgImage = Image.FromStream(imageStream);
                        }
                    }
                    string sFileExtension = _siteHeader.FileName.Substring(_siteHeader.FileName.IndexOf('.') + 1, 

_siteHeader.FileName.Length - (_siteHeader.FileName.IndexOf('.') + 1));

                    Response.ContentType = Utility.GetContentTypeByExtension(sFileExtension.ToLower());
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BufferOutput = false;
                    if (imgImage != null)
                    {
                         ImageFormat _imageFormat = Utility.GetImageFormat(sFileExtension.ToLower());
                         imgImage.Save(Response.OutputStream, _imageFormat);
                         imgImage.Dispose();
                    }
              }
           }

       return new EmptyResult();
    }

我上传原始图片时工作正常。但是当我上传任何下载的图像时,它会抛出以下错误:

It works fine when i upload original image. But when i upload any downloaded images, it throws the following error:

 System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)

For。例如:当我上传原始图像时,它在我的网站中显示为徽标,我从网站上下载了该徽标,当我重新上传相同的下载图像时,它会抛出上述错误。这对我来说似乎很奇怪,而且无法找到它发生的原因。有什么想法吗?

For. Ex: When i upload the original image, it shows as logo in my site and i downloaded that logo from the site and when i re-upload the same downloaded image, it throws the above error. It seems very weird to me and not able to find why its happening. Any ideas on this?

推荐答案

我猜你的问题就在这里:

I'd guess that your problem lies here:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 

    { 
      imgImage = Image.FromStream(imageStream); 
    } 

因为在使用.FromStream之后,Image拥有了流,可能会非常沮丧如果你关闭它。要验证是否存在问题,您可以尝试:

Because after using .FromStream, the Image owns the stream and might be very upset if you close it. To verify if that's the problem you can just try:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 
{ 
  imgImage = new Bitmap( Image.FromStream(imageStream) ); 
} 

这篇关于“GDI +中发生了一般性错误”显示上传的图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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