Codingitup
asked on
A generic error occurred in GDI+. when uploaded to webserver
Hi,
I'm having trouble saving pictures. I take a picture using the below code, re-size it and save it to a location within the asp.net web application. It works great in development but it comes up with an error on line once uploaded to the webserver: -
resizedImage.Save(tempFile Name, uploadedImage.RawFormat);
The code is: -
The error message it comes back with is: -
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.InteropServ ices.Exter nalExcepti on: 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.
Stack Trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save( String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +752921
PromptedInspConfig.PI.Setu pQuestions .RunUpload 1_Click(Ob ject sender, ImageClickEventArgs e) in Z:\SourceCode\PromptedConf ig\PI\Setu pQuestions .aspx.cs:8 60
System.Web.UI.WebControls. ImageButto n.OnClick( ImageClick EventArgs e) +187
System.Web.UI.WebControls. ImageButto n.RaisePos tBackEvent (String eventArgument) +165
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +3707
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
What am I doing wrong please?
Many Thanks
Lee
I'm having trouble saving pictures. I take a picture using the below code, re-size it and save it to a location within the asp.net web application. It works great in development but it comes up with an error on line once uploaded to the webserver: -
resizedImage.Save(tempFile
The code is: -
// Validate the uploaded file
// 1. The uploaded file should not be a blank file
if (fuQuestion1.FileBytes.Length == 0)
return;
// 2. The uploaded file should be having one of the allowed extensions
string ext = System.IO.Path.GetExtension(fuQuestion1.FileName).TrimStart(".".ToCharArray()).ToLower();
if ((ext != "jpeg") && (ext != "jpg") && (ext != "JPG") && (ext != "Jpg") && (ext != "JPEG") && (ext != "Jpeg"))
{
return;
}
// Validation successful
// Load the image into Bitmap Object
Bitmap uploadedImage = new Bitmap(fuQuestion1.FileContent);
// Set the maximum width and height here.
// You can make this versatile by getting these values from
// QueryString or textboxes
int maxWidth = 480;
int maxHeight = 0;
// Resize the image
Bitmap resizedImage = GetScaledPicture(uploadedImage, maxWidth, maxHeight);
//Save the image
String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
String tempFileName = Server.MapPath(virtualPath);
resizedImage.Save(tempFileName, uploadedImage.RawFormat);
// Load the resized image on the browser
hfQuestion1.Value = virtualPath;
imgPreview1.ImageUrl = virtualPath;
The error message it comes back with is: -
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.InteropServ
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.
Stack Trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(
PromptedInspConfig.PI.Setu
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.Process
--------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
What am I doing wrong please?
Many Thanks
Lee
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.