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 in Image.save From PictureBox

Error in Image.save From PictureBox

Scheduled Pinned Locked Moved C#
databasesql-servergraphicssysadminhelp
4 Posts 3 Posters 7 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.
  • F Offline
    F Offline
    Ferec Bruno
    wrote on last edited by
    #1

    in a pictureBox.Image, i put an picture by using memoryStream from a dataset connected to Sql Server. when i navigate by a scrollBar from one record to another, picture is well displayed in the control (setting to null value if no image in datarow, or reading memorystream if present) if i modify picture by paste a new image or by reading a disk picture, saving it to another memorystream (or direct to disk also) by using image.save(stream,ImageFormat.xxx) or Image.Save("filename") is OK but if i DON'T change picturebox.image and try an Image.Save() i get this : System.InteropServices.ExternalException generic error in GDI + image length is zero kByte on disk (not writing) or corrupted in memoryStream... I don't understand !! :(( an Idea ?? thanks Bruno Ferec (bferec@eni-consulting.fr) Technical manager

    A 1 Reply Last reply
    0
    • F Ferec Bruno

      in a pictureBox.Image, i put an picture by using memoryStream from a dataset connected to Sql Server. when i navigate by a scrollBar from one record to another, picture is well displayed in the control (setting to null value if no image in datarow, or reading memorystream if present) if i modify picture by paste a new image or by reading a disk picture, saving it to another memorystream (or direct to disk also) by using image.save(stream,ImageFormat.xxx) or Image.Save("filename") is OK but if i DON'T change picturebox.image and try an Image.Save() i get this : System.InteropServices.ExternalException generic error in GDI + image length is zero kByte on disk (not writing) or corrupted in memoryStream... I don't understand !! :(( an Idea ?? thanks Bruno Ferec (bferec@eni-consulting.fr) Technical manager

      A Offline
      A Offline
      apferreira
      wrote on last edited by
      #2

      While working on an Image Processing App I encountered the same kind of generic error while using the Bitmap.Save() method. I did some searches in the www and it turns out that the Image/Bitmap class uses some type of on-demand loading scheme that requires the file to be open all through the object's life (therefore minimizing the amount of memory being used by big images) This creates several problems. For instance, when you call the Image.Save("filename") to save the image to same file from where it was loaded, your program crashes because it is trying to overwrite an open file. One workaround for this would be to save the changes to another file or to use unmanaged code to load all the image to memory.

      S 1 Reply Last reply
      0
      • A apferreira

        While working on an Image Processing App I encountered the same kind of generic error while using the Bitmap.Save() method. I did some searches in the www and it turns out that the Image/Bitmap class uses some type of on-demand loading scheme that requires the file to be open all through the object's life (therefore minimizing the amount of memory being used by big images) This creates several problems. For instance, when you call the Image.Save("filename") to save the image to same file from where it was loaded, your program crashes because it is trying to overwrite an open file. One workaround for this would be to save the changes to another file or to use unmanaged code to load all the image to memory.

        S Offline
        S Offline
        Steven Behnke
        wrote on last edited by
        #3

        There is a much simpler solution. // Create a memory stream MemoryStream mStream = new MemoryStream(); // Save the image to the memory stream Image.Save(mStream, ...); // Then write the buffer to a file. fileStream.Write(mStream.GetBytes(), 0, mStream.Length); I'm doing this off the top of my head, so the syntax may be slightly off, but the concept is solid. I use it all the time. Thanks, Steven

        A 1 Reply Last reply
        0
        • S Steven Behnke

          There is a much simpler solution. // Create a memory stream MemoryStream mStream = new MemoryStream(); // Save the image to the memory stream Image.Save(mStream, ...); // Then write the buffer to a file. fileStream.Write(mStream.GetBytes(), 0, mStream.Length); I'm doing this off the top of my head, so the syntax may be slightly off, but the concept is solid. I use it all the time. Thanks, Steven

          A Offline
          A Offline
          apferreira
          wrote on last edited by
          #4

          Yes, I have also used that technique of caching the Bitmap in a memoryStream. You can then dispose the bitmap object and work only with the memoryStream. The only problem is that when working with large images (as was the case of my image processing program) this imposes a great cost in terms of memory: 1. All the bitmap info is in memory 2.Each time you want to display your bitmap in the screen you have to do something like pictureBox1.Image=Bitmap.FromStream(mStream,...) So this is only a solution if the user has a lot of free memory or if the images he is working with are small.

          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