when uploading an image to the website i get the following error message:

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) +377630
   System.Drawing.Image.Save(String filename, ImageFormat format) +69
   gg_portfolio.galleryData.Button1_Click(Object sender, EventArgs e) in C:\Users\Barrie\Desktop\GG_portfolio\gg_portfolio\gg_portfolio\galleryData.aspx.cs:140
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

code behind

protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string savefileImage; //image name
                string photo_url; //binary value here
                string photo_title = (Convert.ToString(txtImgName.Text).Trim());
                int returnValue = RandomNumber(5, 20) + RandomNumber(50, 99999); //two random numbers genertated
                string date = (Convert.ToString(DateTime.Now.ToString("HHmmss"))); //current time is generated
                string NameNum = returnValue + date; //random number and time here
                int? imgID = 0; //image ID (replaced with value from database)

                if (FileUpload1.HasFile) //if an image has been selected
                {
                    try
                    {
                        if (photo_title == "") //if the user dose not name the file.
                        {
                            photo_title = (Convert.ToString(Path.GetFileNameWithoutExtension(FileUpload1.FileName).Trim())); //gets image name
                            var length = photo_title.Length;

                            if (length > 25)
                            {
                                var value = (length - 25);
                                photo_title = photo_title.Remove(25, value); //after the first 25 char, remove the 'value' of charaters 
                            }
                        }
                        else //if the user name the file.
                        {
                            var length = photo_title.Length;

                            if (length > 25)
                            {
                                var value = (length - 25);
                                photo_title = photo_title.Remove(25, value); //after the first 25 char, remove the 'value' of charaters 
                            }
                        }

                        byte[] photo_bytes = FileUpload1.FileBytes;//file size(not being used in database atm)

                        // Get the bitmap data from the uploaded file
                        Bitmap src = Bitmap.FromStream(FileUpload1.PostedFile.InputStream) as Bitmap;


                        //http://www.codeproject.com/KB/aspnet/netimageupload.aspx


                        //save product image start
                        int imageH = 1024; //image height
                        int imageW = 768; //image width

                        Bitmap result2 = ResizeBitmap(src, imageW, imageH);//resize the image to a large image

                        MemoryStream memStream = new MemoryStream(); // create a new memory stream
                        result2.Save(memStream, ImageFormat.Jpeg); // save large image into memory
                        byte[] large = memStream.ToArray(); //Convert to byte array 

                        //renames the image file to a random name
                        savefileImage = (photo_url = (Convert.ToString(FileUpload1.FileName.Replace(FileUpload1.FileName, "image_" + NameNum + ".jpeg"))));

                        result2.Save(savefileImage, ImageFormat.Jpeg); // sets image format to a jpeg
                        //save product image end

                        //make thumbnail of photo
                        int thumbHeight = 300; //thumbnail height
                        int thumbWidth = 300; //thumbnail width
                        Bitmap resultThumb = ResizeBitmap(src, thumbWidth, thumbHeight);//resize the image to a large image

                        memStream.SetLength(0); //clear the memory stream
                        resultThumb.Save(memStream, ImageFormat.Jpeg); //save thumbnail into memory
                        byte[] small = memStream.ToArray(); //Convert to byte array 



                        var datetime = DateTime.Now;
                        using (api_gallery_dataDataContext api = new api_gallery_dataDataContext()) //inserts image details in to the products_img database
                        {
                            if (Request.QueryString["gallery_id"] != null)
                            {
                                int gallery_id = (Convert.ToInt32(Request.QueryString["gallery_id"])); //get category id

                                var result = api.insertPhoto
                                        (
                                         gallery_id,
                                         photo_title,
                                         photo_url,
                                         datetime,
                                         new System.Data.Linq.Binary(large),
                                         new System.Data.Linq.Binary(small),
                                         ref imgID //ref imgID is returned from a stored procedure (returns the ID it creates when saving the image).
                                         );
                            }
                        }

                        Page.Response.Redirect(Page.Request.Url.ToString(), true); //reload the current page                  
                        txtImgName.Text = "";

                    }
                    catch
                    {
                        return;
                    }
                }

            }

        }

it works fine running it locally but uploading it to a web server i get the error, researching the error some people have said it could be to do with the memoryStream? iv tried to change my code but nothing seams to work? any ideas?

ok so finally found out what the issue was, it was a simple solution as usual, it was due to this piece of code

result2.Save(memStream, ImageFormat.Jpeg); // save large image into memory
                        byte[] large = memStream.ToArray(); //Convert to byte array 
 
                        .......code omitted......
 
result2.Save(savefileImage, ImageFormat.Jpeg);

due to the result2.save being executed twice I was getting the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.