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. .NET (Core and Framework)
  4. not able to save image which is already saved with same name and to the same location

not able to save image which is already saved with same name and to the same location

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicswinformssysadminhelptutorial
6 Posts 3 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.
  • V Offline
    V Offline
    ven753
    wrote on last edited by
    #1

    hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.

    T P 2 Replies Last reply
    0
    • V ven753

      hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.

      T Offline
      T Offline
      TnTinMn
      wrote on last edited by
      #2

      First make sure you are disposing the old image instance. If the image was loaded from a file, GDI keeps lock on the file until it is disposed. see : Bitmap and Image constructor dependencies for a way to work around this. see: http://stackoverflow.com/a/13496652[^] for a good explanation of GDI+ memory use.

      V 1 Reply Last reply
      0
      • T TnTinMn

        First make sure you are disposing the old image instance. If the image was loaded from a file, GDI keeps lock on the file until it is disposed. see : Bitmap and Image constructor dependencies for a way to work around this. see: http://stackoverflow.com/a/13496652[^] for a good explanation of GDI+ memory use.

        V Offline
        V Offline
        ven753
        wrote on last edited by
        #3

        I have tried like this copying to new bitmap then disposing old bitmap and then saving new bitmap using below code if (System.IO.File.Exists(serpath)) { System.Drawing.Image myimage1; myimage1 = System.Drawing.Image.FromFile(serpath); System.Drawing.Bitmap source_bitmap = new System.Drawing.Bitmap(myimage1); myimage1.Dispose(); int thumb_width = 400, thumb_height = 100; System.Drawing.Bitmap thumb_bitmap = new System.Drawing.Bitmap(thumb_width, thumb_height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb_bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(System.Drawing.Brushes.White, 0, 0, thumb_width, thumb_height); g.DrawImage(source_bitmap, 0, 0, thumb_width, thumb_height); g.Dispose(); System.Drawing.Imaging.ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb_bitmap.Save(serpath, Info[1], Params); } else { signatureImage.Save(serpath, ImageFormat.Jpeg); } but then also it is giving error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+ and not able to delete that file also. how to do this. Please reply me, thanks in advance.

        T 1 Reply Last reply
        0
        • V ven753

          I have tried like this copying to new bitmap then disposing old bitmap and then saving new bitmap using below code if (System.IO.File.Exists(serpath)) { System.Drawing.Image myimage1; myimage1 = System.Drawing.Image.FromFile(serpath); System.Drawing.Bitmap source_bitmap = new System.Drawing.Bitmap(myimage1); myimage1.Dispose(); int thumb_width = 400, thumb_height = 100; System.Drawing.Bitmap thumb_bitmap = new System.Drawing.Bitmap(thumb_width, thumb_height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb_bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(System.Drawing.Brushes.White, 0, 0, thumb_width, thumb_height); g.DrawImage(source_bitmap, 0, 0, thumb_width, thumb_height); g.Dispose(); System.Drawing.Imaging.ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb_bitmap.Save(serpath, Info[1], Params); } else { signatureImage.Save(serpath, ImageFormat.Jpeg); } but then also it is giving error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+ and not able to delete that file also. how to do this. Please reply me, thanks in advance.

          T Offline
          T Offline
          TnTinMn
          wrote on last edited by
          #4

          I learned something new; the BitMap(Image img) constructor does all of that painting of the source Image to a new BitMap for you. Cool! :-D :-D Now for your issue. Give this a try. I rewrote the logic to get the JPEG encoder assuming that is the one you wanted. If not, it is easy to change and understand now. There should really be some handling in there in the unlikely event that the encoder does not exist. But you can add that easily enough.

          if (System.IO.File.Exists(serpath))
          {

          System.Drawing.Image myimage1 = System.Drawing.Image.FromFile(serpath);
          
          int thumb\_width = 400, thumb\_height = 100;
          using (System.Drawing.Bitmap thumb\_bitmap = new System.Drawing.Bitmap(thumb\_width, thumb\_height))
          {
              using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb\_bitmap))
              {
                  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                  g.Clear(Color.White);
                  g.DrawImage(myimage1, 0, 0, thumb\_width, thumb\_height);
              }
              myimage1.Dispose();
              System.Drawing.Imaging.ImageCodecInfo jpeg = default(System.Drawing.Imaging.ImageCodecInfo);
              jpeg = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders().
                     Where((System.Drawing.Imaging.ImageCodecInfo enc) => 
                         enc.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
                         .FirstOrDefault();
          
          
              System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
              Params.Param\[0\] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
          
              thumb\_bitmap.Save(serpath, jpeg, Params);
          
          }
          

          }
          else
          {
          signatureImage.Save(serpath, System.Drawing.Imaging.ImageFormat.Jpeg);
          }
          }

          V 1 Reply Last reply
          0
          • T TnTinMn

            I learned something new; the BitMap(Image img) constructor does all of that painting of the source Image to a new BitMap for you. Cool! :-D :-D Now for your issue. Give this a try. I rewrote the logic to get the JPEG encoder assuming that is the one you wanted. If not, it is easy to change and understand now. There should really be some handling in there in the unlikely event that the encoder does not exist. But you can add that easily enough.

            if (System.IO.File.Exists(serpath))
            {

            System.Drawing.Image myimage1 = System.Drawing.Image.FromFile(serpath);
            
            int thumb\_width = 400, thumb\_height = 100;
            using (System.Drawing.Bitmap thumb\_bitmap = new System.Drawing.Bitmap(thumb\_width, thumb\_height))
            {
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb\_bitmap))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.Clear(Color.White);
                    g.DrawImage(myimage1, 0, 0, thumb\_width, thumb\_height);
                }
                myimage1.Dispose();
                System.Drawing.Imaging.ImageCodecInfo jpeg = default(System.Drawing.Imaging.ImageCodecInfo);
                jpeg = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders().
                       Where((System.Drawing.Imaging.ImageCodecInfo enc) => 
                           enc.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)
                           .FirstOrDefault();
            
            
                System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
                Params.Param\[0\] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
            
                thumb\_bitmap.Save(serpath, jpeg, Params);
            
            }
            

            }
            else
            {
            signatureImage.Save(serpath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            }

            V Offline
            V Offline
            ven753
            wrote on last edited by
            #5

            Actually using this code the blank image is coming. Using my previous code only i am getting updated image to the same location with the same name. Only problem is after sending mail with this image iinked to the html and after html mail delivery, i am not able to again save new updated image with same name and to the same location. How to resolve this. Please reply me. Thanks in advance.

            1 Reply Last reply
            0
            • V ven753

              hi in web application I have saved one bitmap image to serverpath and once again if i try to save another image with the same name to the same server path i am getting follwing error 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. and also if i try to delete that image file with the same path using below code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this.i need to overwrite the image with the same name and to the same location and also delete. how to resolve this. if anybody knows,Please Reply me. thanks in advance.

              P Offline
              P Offline
              PKJain75
              wrote on last edited by
              #6

              hello Please share us complete details i.e how you are displaying an images on site. as this is due to image object is not getting null after rendring images and it is till in use at server.

              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