views:

79

answers:

2

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?

+2  A: 

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) ); 
} 
danbystrom
the code"imgImage = new Image( Image.FromStream(imageStream) );" gives error. = Cannot create an instance of the abstract class or interface 'System.Drawing.Image'
Prasad
Sorry. Bitmap of course. :-(
danbystrom
imgImage = new Bitmap( Image.FromStream(imageStream) ); worked perfect. Also When i used "imgImage.Save(Response.OutputStream, _imageFormat);" inside "using" method of my existing code, it worked.
Prasad
+1  A: 

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.

Matt Sherman
The Image is coming from the database, and its not in any real path. I am getting this error, even when i am working on localhost.
Prasad