locked
Image save/resize causing generic error in GDI+ RRS feed

  • Question

  • User-263234033 posted

    I'm not sure why i'm getting the Generic error in GDI+ error when i try to resize and save an image. Most of the causes of this error i have found online stem from closing the stream too soon, I do not close the stream so i'm not sure why this error is being thrown...below i will post the full error msg and my code, btw this code was working perfect for the past week, just starting acting up now.

     

    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:

    Line 133:        System.Drawing.Image imgInput = System.Drawing.Image.FromStream(imgStream);
    Line 134: Bitmap bmpResized = new Bitmap(imgInput, 200, 150);
    Line 135: bmpResized.Save(savepath, ImageFormat.Jpeg);
    Line 136: }
    Line 137:}

    Source File: d:\hosting\3500855\html\AddHats.aspx.cs    Line: 135

    Stack Trace:

    [ExternalException (0x80004005): A generic error occurred in GDI+.]
    System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +397778
    System.Drawing.Image.Save(String filename, ImageFormat format) +69
    Admin_AddHats.Process_Image(Stream imgStream, String savepath) in d:\hosting\3500855\html\AddHats.aspx.cs:135
    Admin_AddHats.UploadSmallFile(FileUpload upload, String ty) in d:\hosting\3500855\html\AddHats.aspx.cs:115
    Admin_AddHats.addHat(Int32 id, String name, String desc, Int32 type, String sports, Double price, String lrg_photo) in d:\hosting\3500855\html\AddHats.aspx.cs:98
    Admin_AddHats.submit_btn_Click(Object sender, EventArgs e) in d:\hosting\3500855\html\AddHats.aspx.cs:43
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746


    Version Information: Microsoft .NET Framework Version:2.0.50727.1434; ASP.NET Version:2.0.50727.1434 

     

     

    and my code...

     

    protected void submit_btn_Click(object sender, EventArgs e)
        {
            int id = 0;
            string sflag = "No";

            foreach(GridViewRow row in GridView1.Rows)
            {
                id = Convert.ToInt32(row.Cells[0].Text) + 1;
            }

            if(sports_chk.Checked == true)
            {
                sflag = "Yes";
            }
            else
            {
                sflag = "No";
            }

            addHat(id, name_txt.Text, desc_txt.Text, Convert.ToInt32(type_drop.SelectedValue), sflag, Convert.ToDouble(price_txt.Text), lrgPic_upl.PostedFile.FileName);

            GridView1.DataBind();
            name_txt.Text = "";
            desc_txt.Text = "";
            sports_chk.Checked = false;
            price_txt.Text = "";
        }
        public void addHat(int id, string name, string desc, int type, string sports, double price, string lrg_photo)
        {
            string ty = "";

            switch (type)
            {
                case 1:
                    ty = "Ponytail";
                    break;
                case 2:
                    ty = "Slimlid";
                    break;
                case 3:
                    ty = "Tieback";
                    break;
            }

            string sm_photo_path = "~\\sitePics\\" + ty + "\\" + lrg_photo;
            string lrg_photo_path = "~\\sitePics\\" + ty + "\\Large\\" + lrg_photo;
              
                SqlConnection sql = new SqlConnection();
                sql.ConnectionString = "Server=[server]; Database=[DB]; User ID=[userid]; Password=[password]; Trusted_Connection=False";
                sql.Open();
               
               
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = sql;
                cmd.Parameters.Add("ident", SqlDbType.Int, 20).Value = id;
                cmd.Parameters.Add("nme", SqlDbType.VarChar, 25).Value = name;
                cmd.Parameters.Add("desc", SqlDbType.VarChar, 100).Value = desc;
                cmd.Parameters.Add("sprts", SqlDbType.VarChar, 25).Value = sports;
                cmd.Parameters.Add("type", SqlDbType.Int, 4).Value = type;
                cmd.Parameters.Add("price", SqlDbType.SmallMoney, 50).Value = price;
                cmd.Parameters.Add("lrgpho", SqlDbType.VarChar, lrg_photo_path.Length).Value = lrg_photo_path;
                cmd.Parameters.Add("smpho", SqlDbType.VarChar, sm_photo_path.Length).Value = sm_photo_path;
               
                cmd.CommandText = "INSERT INTO HAT VALUES(@ident, @nme, @desc, @sprts, 'No', @type, @price, @lrgpho, @smpho, 'No')";

                cmd.ExecuteNonQuery();
               
                cmd.Dispose();
                sql.Close();
                sql.Dispose();

           
                UploadSmallFile(lrgPic_upl, ty);
                UploadLargeFile(lrgPic_upl, ty);

               

        }

        protected void UploadSmallFile(FileUpload upload, string ty)
        {
            if (upload.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/sitePics/HatPics/"+ty), upload.FileName);
               
                Process_Image(upload.PostedFile.InputStream, theFileName);
            }
        }

        protected void UploadLargeFile(FileUpload upload, string ty)
        {
            if (upload.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/sitePics/HatPics/" + ty + "/Large"), upload.FileName);
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                upload.SaveAs(theFileName);
            }
        }
        private void Process_Image(Stream imgStream, String savepath)
        {
            System.Drawing.Image imgInput = System.Drawing.Image.FromStream(imgStream);
            Bitmap bmpResized = new Bitmap(imgInput, 200, 150);
            bmpResized.Save(savepath, ImageFormat.Jpeg);
        }

     

     

    Sorry for the large block of code, thought it all could be relevant...Help is much appreciated.

     

    Brian.

     

    Wednesday, December 10, 2008 1:59 PM

Answers