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