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. How to save a bitmap

How to save a bitmap

Scheduled Pinned Locked Moved C#
graphicshelpwinformstutorialquestion
5 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.
  • P Offline
    P Offline
    PrebKlok
    wrote on last edited by
    #1

    I am trying to save at bitmap that has been loaded with bitmap = (Bitmap) Bitmap.FromFile(FileName); When I try to save it again with bitmap.Save(FileName); I get an error message: An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+. If I try to it save with bitmap.Save(FileName2); everyting is fine, but it's not ok to me that you have to give the bitmap a new name. It's like the file is blocked by the system. I have noticed that the Image Processing for Dummies by Christian Graus have the same problem. How to solve that?

    A 1 Reply Last reply
    0
    • P PrebKlok

      I am trying to save at bitmap that has been loaded with bitmap = (Bitmap) Bitmap.FromFile(FileName); When I try to save it again with bitmap.Save(FileName); I get an error message: An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+. If I try to it save with bitmap.Save(FileName2); everyting is fine, but it's not ok to me that you have to give the bitmap a new name. It's like the file is blocked by the system. I have noticed that the Image Processing for Dummies by Christian Graus have the same problem. How to solve that?

      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. If the bitmap is not too big you might also consider caching the bitmap in memory, ie: 1. Open the bitmap 2. Do a Bitmap.Save to a memoryStream 3. Dispose of the bitmap object and work with the memorystream 4.When your work is finished save the memorystream to a file. But be carefull with this last approach when working with big bitmaps (big overhead)

      P 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. If the bitmap is not too big you might also consider caching the bitmap in memory, ie: 1. Open the bitmap 2. Do a Bitmap.Save to a memoryStream 3. Dispose of the bitmap object and work with the memorystream 4.When your work is finished save the memorystream to a file. But be carefull with this last approach when working with big bitmaps (big overhead)

        P Offline
        P Offline
        PrebKlok
        wrote on last edited by
        #3

        Well - what is big bitmaps? Mine is about 3000px x 2000px, and i'm making the program as mdi, so there could be up to 20 bitmaps loaded into memory, but of cause you only would have to save one bitmap at the time. Anyway, i'm kind of new to c# (only been programming for a few weeks), so i'm not sure about what memoryStream i about yet. Might the be any othe solutions?

        N 1 Reply Last reply
        0
        • P PrebKlok

          Well - what is big bitmaps? Mine is about 3000px x 2000px, and i'm making the program as mdi, so there could be up to 20 bitmaps loaded into memory, but of cause you only would have to save one bitmap at the time. Anyway, i'm kind of new to c# (only been programming for a few weeks), so i'm not sure about what memoryStream i about yet. Might the be any othe solutions?

          N Offline
          N Offline
          Nick Seng
          wrote on last edited by
          #4

          Well, How about this way? 1. save the bitmap using another name, say temp 2. Delete the old bitmap file 3. rename temp to the old bitmap name. I realize that this is probably a long way around, but it'll probably save memory for thos big bitmaps Notorious SMC


          The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
          Get your facts first, and then you can distort them as much as you please Mark Twain

          P 1 Reply Last reply
          0
          • N Nick Seng

            Well, How about this way? 1. save the bitmap using another name, say temp 2. Delete the old bitmap file 3. rename temp to the old bitmap name. I realize that this is probably a long way around, but it'll probably save memory for thos big bitmaps Notorious SMC


            The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
            Get your facts first, and then you can distort them as much as you please Mark Twain

            P Offline
            P Offline
            PrebKlok
            wrote on last edited by
            #5

            Thanks, I will try that solution. I have just testet the method with memoryStream - it works, but it used 600 MB of memory!!! I only have 256 MB RAM, so my harddisk went totaly crazy, and even after I had shut down the program, Windows has totaly wierd.

            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