“A generic error occurred in GDI+” error while showing uploaded images

99封情书 提交于 2019-12-10 11:45:34

问题


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. 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?


回答1:


I'd guess that your problem lies here:

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

    { 
      imgImage = Image.FromStream(imageStream); 
    } 

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



回答2:


I've found that error usually comes from a file access problem. Sounds obvious, I realize, but double-check that the file path is correct and that the file exists, and also that the IIS process has permissions to that file.



来源:https://stackoverflow.com/questions/3003962/a-generic-error-occurred-in-gdi-error-while-showing-uploaded-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!