Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error: A Generic Error occured in GDI+

Error: A Generic Error occured in GDI+

Scheduled Pinned Locked Moved C#
graphicswinformssysadminhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Udayaraju
    wrote on last edited by
    #1

    Hi Dear, Here I am trying to upload a photo.

    protected void LoadImage() {
    string UserName = Session["UserName"].ToString();

            byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class
           
            if (Data != null)
            {
                MemoryStream stream = new MemoryStream(Data);
    
                Bitmap bitmap = null;
                bitmap = new Bitmap(stream);
    
                bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); 
                //Error:A Generic   Error occurred in GDI+            
                bitmap.Dispose();
                imgPhoto.ImageUrl = @"~\\UserPhoto.bmp";            }     }
    

    I have created virtual directory and trying to open it directly from browser

    L L B 3 Replies Last reply
    0
    • U Udayaraju

      Hi Dear, Here I am trying to upload a photo.

      protected void LoadImage() {
      string UserName = Session["UserName"].ToString();

              byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class
             
              if (Data != null)
              {
                  MemoryStream stream = new MemoryStream(Data);
      
                  Bitmap bitmap = null;
                  bitmap = new Bitmap(stream);
      
                  bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); 
                  //Error:A Generic   Error occurred in GDI+            
                  bitmap.Dispose();
                  imgPhoto.ImageUrl = @"~\\UserPhoto.bmp";            }     }
      

      I have created virtual directory and trying to open it directly from browser

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, what is that tilde doing there? Here is a standard reply I found in the attic: Most, if not all, errors inside GDI+ are reported as "generic problem occurred in GDI+". If the affected line is an Image.Save chances are your path is incorrect or inaccessible, your disk is full, or your destination file exists and is locked. If you load an image from a file, most of the time the file remains locked as long as the Image is alive. This would prevent you from saving an image to the same path. It applies to Image.FromFile, and probably also to PictureBox.ImageLocation The one exception I am aware of is when you use Image.FromStream. So I suggest one of these two workarounds:

      Bitmap bm=null;
      {
      Bitmap bm1=Image.FromFile(filepath);
      bm=new Bitmap(bm1);
      bm1.Dispose();
      }
      // bm is OK now, and bm1 is disposed of and out of scope, the file isn’t locked.

      or

      Bitmap bm=null;
      using (FileStream stream=File.OpenRead("image.jpeg")) {
      bm=Image.FromStream(stream);
      }

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      B 1 Reply Last reply
      0
      • U Udayaraju

        Hi Dear, Here I am trying to upload a photo.

        protected void LoadImage() {
        string UserName = Session["UserName"].ToString();

                byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class
               
                if (Data != null)
                {
                    MemoryStream stream = new MemoryStream(Data);
        
                    Bitmap bitmap = null;
                    bitmap = new Bitmap(stream);
        
                    bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); 
                    //Error:A Generic   Error occurred in GDI+            
                    bitmap.Dispose();
                    imgPhoto.ImageUrl = @"~\\UserPhoto.bmp";            }     }
        

        I have created virtual directory and trying to open it directly from browser

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        First you should be very careful when you use GDI+ combined with ASP.NET.Microsoft warns that some GDI+ features may will cause some unexpected problems when are used with ASP.NET. Why don't you write your own HTTP HttpHandler that handles images load from the database for example?

        Life is a stage and we are all actors!

        1 Reply Last reply
        0
        • U Udayaraju

          Hi Dear, Here I am trying to upload a photo.

          protected void LoadImage() {
          string UserName = Session["UserName"].ToString();

                  byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class
                 
                  if (Data != null)
                  {
                      MemoryStream stream = new MemoryStream(Data);
          
                      Bitmap bitmap = null;
                      bitmap = new Bitmap(stream);
          
                      bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); 
                      //Error:A Generic   Error occurred in GDI+            
                      bitmap.Dispose();
                      imgPhoto.ImageUrl = @"~\\UserPhoto.bmp";            }     }
          

          I have created virtual directory and trying to open it directly from browser

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          Along the lines of the other recommendations, taking the image data and constructing a Bitmap of it seems to be extraneous. If you have the image data already, then there is no benefit in using that data to construct a Bitmap other than being able to invoke Bitmap.Save(). All you really need to do is write the data directly to disk:

          using (FileStream fs = new FileStream(Server.MapPath(@"~\UserPhoto.bmp"), FileMode.CreateNew))
          {
          fs.Write(Data, 0, Data.Length);
          }

          This also has the benefit of removing GDI from the equation. So, if you still get an exception you will atleast be able to catch it as some sort of IO Exception and get more meaningful information about the problem.

          Hold on a second here... Don't you think you might be putting the horse ahead of the cart?

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, what is that tilde doing there? Here is a standard reply I found in the attic: Most, if not all, errors inside GDI+ are reported as "generic problem occurred in GDI+". If the affected line is an Image.Save chances are your path is incorrect or inaccessible, your disk is full, or your destination file exists and is locked. If you load an image from a file, most of the time the file remains locked as long as the Image is alive. This would prevent you from saving an image to the same path. It applies to Image.FromFile, and probably also to PictureBox.ImageLocation The one exception I am aware of is when you use Image.FromStream. So I suggest one of these two workarounds:

            Bitmap bm=null;
            {
            Bitmap bm1=Image.FromFile(filepath);
            bm=new Bitmap(bm1);
            bm1.Dispose();
            }
            // bm is OK now, and bm1 is disposed of and out of scope, the file isn’t locked.

            or

            Bitmap bm=null;
            using (FileStream stream=File.OpenRead("image.jpeg")) {
            bm=Image.FromStream(stream);
            }

            :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            B Offline
            B Offline
            Ben Fair
            wrote on last edited by
            #5

            In ASP.NET the tilde at the beginning of a path represents the local file system path to the root of the application. Without it you'd have no way for the server to reliably access the local file system relevant to the application. So, for example if I have an ASP.NET application on my local machine named "TestApp", when I call Server.MapPath("~"); I would get something like "C:\Inetpub\wwwroot\TestApp".

            Hold on a second here... Don't you think you might be putting the horse ahead of the cart?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups