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