开发者

"A generic error occurred in GDI+" error while showing uploaded images

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 (_site开发者_开发问答Header.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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜