Click here to Skip to main content
15,881,588 members
Articles / Multimedia / GDI+
Article

Compressible Bitmap Using In-Memory Image Compressing Technique

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
14 Mar 2007CPOL3 min read 77.7K   5.9K   40   4
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.

ConstructorsCompressibleImage(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.
MethodsGetDecompressedImage()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.
PropertyIsCompressedGets a value indicating if the image is compressed

The two most important methods are:

C#
/// <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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Singapore Singapore
This guy loves computer programming, software design and development. He is interested and specialized in C family languages, especially C#, Java, Objective-C and D Programming Language. Ruby and Python are starting to interest him as well.

Comments and Discussions

 
Questionwhat dpi it will be stored Pin
rush2rajiv19-May-13 21:52
rush2rajiv19-May-13 21:52 
QuestionThis works really well ! Pin
cronky7817-Jul-12 16:48
cronky7817-Jul-12 16:48 
GeneralGood Tip! Pin
NormDroid9-Oct-06 4:22
professionalNormDroid9-Oct-06 4:22 
GeneralRe: Good Tip! Pin
karthikeyan12345015-Dec-11 1:36
karthikeyan12345015-Dec-11 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.