RLE (Run-Length-Encoding) Compression Routine in MFC for 256-Color Bitmaps
This is a RLE compression routine, written in MFC. I wrote this because I cannot find handy code for such a thing.
RLE compression is useful when saving a SIMPLE Windows bitmap. My implementation is rather stupid and not optimized. Therefore, if the source bitmap is complex (like the one in the demo app window shown below), you could even get a bigger bitmap file after compression.

Click here for a larger image.
The compression routine is wrapped in an MFC bitmap control class, CDVDBitmapControl, which you can put conveniently in a dialog. The usage is easy:
- Add a static control (say, IDC_STATIC_BITMAP) in the dialog for the bitmap control.
- Add an #include "DVDBitmapControl.h" in the header file of your dialog.
- Add a member variable (say, m_bmp) of type "CDVDBitmapControl" in your dialog class.
- In OnInitDialog(), add this line:
m_bmp.SubclassDlgItem( IDC_STATIC_BITMAP, this). - Call m_bmp.LoadBitmapFile(CString) to load a bitmap into the control.
- Call m_bmp.SaveBitmapFile(CString) to save a bitmap into an 8-bit RLE bitmap.
If you need only the compression code, you can just extract the CDVDBitmapControl::CompressInRLE8() and CDVDBitmapControl::EndOfLine() routines from "DVDBitmapControl.cpp". It is quite straightforward to use them.
I did not write the decompression part because the Windows API LoadImage() already does it.
PS: in the demo app, if the loaded bitmap is not in 256 colors, [Save] will not work.
Downloads
Download demo project - 20 KbDownload source - 5 Kb
More for Developers
Top Authors
- Voted: 13 times.
- Voted: 11 times.
- Voted: 11 times.
- Voted: 8 times.
- Voted: 8 times.
- Paul Kimmel 214 articles
- Zafir Anjum 120 articles
- 15Seconds.com 99 articles
- Tom Archer - MSFT 83 articles
- Jeffrey Juday 82 articles


All