Error:
Server Error in '/' Application.
A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
================
Source Code:
if (productimage.HasFile)
{
string filePath = Server.MapPath(@"ProductImage\");
int _size = 966700;// equal 9 mb
string _fileExt = System.IO.Path.GetExtension(productimage.FileName);
if (_fileExt.ToLower() == ".gif" || _fileExt.ToLower() == ".jpg" || _fileExt.ToLower() == ".png")
{
if (productimage.PostedFile.ContentLength <= _size)
{
string newname = txtproductname.Text.Trim();
string extension = Path.GetExtension(productimage.PostedFile.FileName);
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(productimage.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = 1;
if (origHeight < origWidth)
{
sngRatio = origWidth / origHeight;
}
int newWidth = 100;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(filePath + "Pro_" + newname + extension, System.Drawing.Imaging.ImageFormat.Bmp);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
lblimgpath.Text = @"ProductImage/Pro_" + newname + extension;
}
}
}
{
string filePath = Server.MapPath(@"ProductImage\");
int _size = 966700;// equal 9 mb
string _fileExt = System.IO.Path.GetExtension(productimage.FileName);
if (_fileExt.ToLower() == ".gif" || _fileExt.ToLower() == ".jpg" || _fileExt.ToLower() == ".png")
{
if (productimage.PostedFile.ContentLength <= _size)
{
string newname = txtproductname.Text.Trim();
string extension = Path.GetExtension(productimage.PostedFile.FileName);
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(productimage.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = 1;
if (origHeight < origWidth)
{
sngRatio = origWidth / origHeight;
}
int newWidth = 100;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(filePath + "Pro_" + newname + extension, System.Drawing.Imaging.ImageFormat.Bmp);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
lblimgpath.Text = @"ProductImage/Pro_" + newname + extension;
}
}
}

Abhay ShankerPosted Apr 30, 2014, 7:25 AM
http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream
http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream
http://weblogs.asp.net/anasghanem/archive/2009/02/28/solving-quot-a-generic-error-occurred-in-gdi-quot-exception.aspx