Reputation: 59303
I'm saving a large amount of bitmaps (screenshots) to memory. Nothing special with the code, it's trivial:
var memory = new MemoryStream();
bitmap.Save(memory, ImageFormat.Png);
Since my PC is getting a bit slow, I have run a performance analysis session in Visual Studio and I found out that the Save()
call takes 37% of "inclusive samples". Another big parts is used when saving to disk, so these 37% are more likely 80% when not saving to disk. (I don't care about saving to disk at the moment. All data is kept in RAM until a hotkey is pressed and I hardly have influence on hard disk speed.)
From my understanding, the Save()
call has to convert the more or less "raw" data of the bitmap into the compressed PNG file format.
I wonder whether someone has a performance overview of the different image formats with respect to the processing time of the Save()
method. I'd like to choose the fastest format, even if the file size is larger.
I have tried:
ImageFormat.MemoryBmp
but that throws an ArgumentNullException
:
Value cannot be null. Parameter name: encoder
I found a related question that describes that some of the image formats are read-only, which reduces the list a bit.
Upvotes: 1
Views: 1302
Reputation: 59303
These are the non-representative results for taking screenshots of 3 monitors on an Intel i7 CPU, where the application is assigned one core only. I was running a x64 release build and saving to a pre-allocated memory buffer.
GIF : ~5.5% CPU load
TIFF: ~4.5% CPU load
PNG : ~4.0% CPU load
JPG : ~2.0% CPU load (note that this is lossy)
BMP : ~1.0% CPU load
I also tried integrating Magick.NET, but since I could not figure out on how to create a Graphics
object from MagickImage
in order to save the screenshot, I had to use the constructor that takes the Bitmap
as argument. This resulted in ~10.0% CPU load for PNG images.
Upvotes: 2