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

Posted on 2012-08-17
Last Modified: 2012-11-22

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 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
Question by:Codingitup
    1 Comment
    LVL 18

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now