1

i have following coding to resize image and than save it my virtual folder "vicpic/scimages"

  if (FileUpload5.PostedFile != null)
    {
        if (FileUpload5.PostedFile.ContentLength > (1024 * 1024))
        {
            Label4.Text = "Upload status: The file has to be less than 1 MB. Please resize your photo and than upload it again.";
        }
        else
        {
            System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload5.PostedFile.InputStream);
            int imageHeight = imageToBeResized.Height;
            int imageWidth = imageToBeResized.Width;
            int maxHeight = 660;
            int maxWidth = 560;
            imageHeight = (imageHeight * maxWidth) / imageWidth;
            imageWidth = maxWidth;
            if (imageHeight > maxHeight)
            {
                imageWidth = (imageWidth * maxHeight) / imageHeight;
                imageHeight = maxHeight;
            }
            Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
            System.IO.MemoryStream stream = new MemoryStream();
            // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;
            byte[] image = new byte[stream.Length + 1];
            stream.Read(image, 0, image.Length);

            string FileName = Path.GetFileName(FileUpload5.PostedFile.FileName);
            //Save files to disk
            string extension = Path.GetExtension(FileUpload5.PostedFile.FileName);
            //Path.Combine(Server.MapPath("~/vicpic/scimages"), imageName);
            string imagename = DropDownList2.SelectedItem.Text + "4" + extension;


            bitmap.Save(Server.MapPath("~/vicpic/scimages/") + imagename);

            bitmap.Dispose();
            imageToBeResized.Dispose();
            GC.Collect();



            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

            finally
            {
                con.Close();
                con.Dispose();
                Label4.Text = "Upload status:Successfully.";

            }
        }

    }

but it shows A generic error occurred in GDI+

can anyone suggest where the problem is?

i want to save image in "vicpic/scimages" folder on my host server. it is virtual directory and i also have granted all the permission required for the directory.

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
Jack
  • 363
  • 1
  • 4
  • 14

2 Answers2

4

I had the same issue. It appears that the memory stream that the object was created on has to be open at the time the object is saved. Not to duplicate code and text just take a look at this Q & A: A generic error occurred in GDI+, JPEG Image to MemoryStream

So instead of bitmap.Save(Server.MapPath("~/vicpic/scimages/") + imagename); your code could look like this:

using (var m = new MemoryStream())
{
       bitmap.Save(m, ImageFormat.Jpeg);
       var img = Image.FromStream(m);
       img.Save(Server.MapPath("~/vicpic/scimages/") + imagename);
}
Community
  • 1
  • 1
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • it shows error in this line: var img = Image.FromStream(m); below error:'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream' – Jack Jul 02 '12 at 12:21
  • 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream' – Jack Jul 02 '12 at 12:24
  • 1
    I see. You need to use another image class. http://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream(v=vs.90).aspx Just add the `System.Drawing` namespace before `Image`. – George Mamaladze Jul 02 '12 at 12:26
  • i have following namespace in the code: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Drawing; – Jack Jul 02 '12 at 12:27
  • Like this `var img = System.Drawing.Image.FromStream(m);` – George Mamaladze Jul 02 '12 at 12:28
0

I know this is an old question but this is just my input that I want to give, if it can help someone with the same issue then that is all I want. I got this error in one of my websites and I had no idea what was going on so after finding some answers on STACK, I realised that it was a permission error. After fixing that everything was fine but then I got the same error on another website of mine and I immediately I changed the permission for the folder how ever this time it didn't fix it . After days of struggle I realised that the issue was just a misspelled folder name.

Julie20
  • 228
  • 1
  • 2
  • 12