• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

A generic error occurred in GDI+. when uploaded to webserver


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(tempFileName, uploadedImage.RawFormat);

The code is: -

                // Validate the uploaded file
                // 1. The uploaded file should not be a blank file
                if (fuQuestion1.FileBytes.Length == 0)
                // 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"))

                // 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;

Open in new window

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.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.  

Stack Trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +752921
   PromptedInspConfig.PI.SetupQuestions.RunUpload1_Click(Object sender, ImageClickEventArgs e) in Z:\SourceCode\PromptedConfig\PI\SetupQuestions.aspx.cs:860
   System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +187
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +165
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +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
1 Solution
Rajar AhmedConsultantCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now