System.Drawing.Bitmap really provides a convenient way for us to create, save and process pictures of many types. However, there is one problem with this class that may not be noticed by all programmers -- it uses a lot of memory resources. For a non-indexed RGB picture of size 1280 by 1024, it will take up 1280 * 1024 * 3 = 3,932,160 = about 4 MB memory. Not so scary, since you have 1 GB memory on your workstation? What if you somehow have to load a thousand pictures of that size into memory at the same time? It will eat up all your physical memory plus virtual memory and then still complain that you don't have enough memory. It's not an imaginary scenario because I encountered the exact problem while writing my application. I'm sure someone would ask: "Why would you want to load so many pictures into memory? You can always load them from disk when they are needed." Unfortunately this doesn't work because loading from disk is too expensive for my application to afford. So I came up with this compromising solution, which takes less loading time than loading from disk and consumes less memory than storing the plain BITMAPs (with the capital BITMAP, I mean the one that stores RGB values in memory).
The concept behind this is very simple. I take that BITMAP, dump it into a stream with some compression method, and save the stream in memory. Whenever the BITMAP is used, I take the stream and load the picture from it. As we all know,
System.Drawing.Bitmap provides a bunch of very cute methods named
Save(...). We can use one of them
Save(Stream, ImageFormat) to serialize our BITMAP into a stream, and the compression method is controlled by the
Using the Code
The solution is coded into a single class
CompressibleImage, which has two constructors, two important methods and one property.
|Creates a |
CompressibleImage instance, given the original image and format for compression.
|Creates a |
CompressibleImage instance, given the stream containing the compressed image.
|Gets the uncompressed image. If the image is compressed, it will be uncompressed first.|
|Clears the uncompressed image, leaving the compressed one in memory.|
|Gets a value indicating if the image is compressed|
The two most important methods are:
public Image GetDecompressedImage()
if (decompressed == null)
decompressed = new Bitmap(stream);
public void ClearDecompressedImage()
if (decompressed != null)
if (stream == null)
stream = new MemoryStream();
decompressed = null;
The following code segment shows how to use this class:
void Usage1(Bitmap b)
CompressibleImage ci = new CompressibleImage(b, ImageFormat.Jpeg);
Image decompressed = ci.GetDecompressedImage();
void Usage2(Bitmap b)
MemoryStream ms = new MemoryStream();
CompressibleImage ci = new CompressibleImage(ms);
Points of Interest
Note that the image quality will be compromised if the
CompressibleImage object is created given a
Bitmap instance and the
ImageFormat.Jpeg format. However, if it is created given a
MemoryStream instance, the quality of
CompressibleImage will remain the same as what's contained in the
MemoryStream. If lossless compression is a must, you should either use the right
ImageFormat or better control the
MemoryStream passed in.
The demo project shows the performance of the
CompressibleImage class. Note that the resulting loading time of the two scenarios are more or less the same. They are due to the OS level or hardware level caching. For very large files or large number of files, the performances will differ.
- 9th October, 2006 - First draft
- 13th March, 2007 - Minor update