"A generic error occurred in GDI+" error while showing uploaded images
- by Prasad
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?