Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Compressible Bitmap Using In-Memory Image Compressing Technique

0.00/5 (No votes)
14 Mar 2007 1  
A wrapper of System.Drawing.Bitmap that serves as a compromising solution in terms of memory usage and loading time.

Introduction

The 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).

Background

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 ImageFormat parameter.

Using the Code

The solution is coded into a single class CompressibleImage, which has two constructors, two important methods and one property.

Constructors CompressibleImage(Bitmap, ImageFormat) Creates a CompressibleImage instance, given the original image and format for compression.
CompressibleImage(MemoryStream) Creates a CompressibleImage instance, given the stream containing the compressed image.
Methods GetDecompressedImage() Gets the uncompressed image. If the image is compressed, it will be uncompressed first.
ClearDecompressedImage() Clears the uncompressed image, leaving the compressed one in memory.
Property IsCompressed Gets a value indicating if the image is compressed

The two most important methods are:

/// <summary>
/// Gets the uncompressed image. If the image is compressed, 
/// it will be uncompressed first.
/// </summary>
public Image GetDecompressedImage()
{
    if (decompressed == null)
    {
        stream.Seek(0, SeekOrigin.Begin);
        decompressed = new Bitmap(stream);
    }
    return decompressed;
}

/// <summary>
/// Clears the uncompressed image, leaving the compressed one in memory.
/// </summary>
public void ClearDecompressedImage()
{
    if (decompressed != null)
    {
        if (stream == null)
        {
            stream = new MemoryStream();
            decompressed.Save(stream, format);
        }
        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();
    //use the decompressed image
    ci.ClearDecompressedImage();
    // make it compressed and use later
}

void Usage2(Bitmap b)
{
    MemoryStream ms = new MemoryStream();
    b.Save(ms, ImageFormat.Jpeg);
    CompressibleImage ci = new CompressibleImage(ms);
    // keep it compressed and use later
} 

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.

History

  • 9th October, 2006 - First draft
  • 13th March, 2007 - Minor update

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here