5,691,626 members and growing! (11,785 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

In Memory Image Compression

By Amer Gerzic

In Memory Image Compression/Decompression
VC6, VC7, VC7.1, C++Windows, NT4, MFC, GDI+, VS6, Visual Studio, Dev

Posted: 15 Aug 2003
Updated: 15 Aug 2003
Views: 84,169
Bookmarked: 25 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 4.68 Rating: 3.66 out of 5
3 votes, 15.8%
1
0 votes, 0.0%
2
1 vote, 5.3%
3
4 votes, 21.1%
4
11 votes, 57.9%
5

Sample Image - Sample.jpg

Introduction

This article is intended to show ONE POSSIBLE way to load a bitmap/jpg/tiff/gif/png (whatever you can load with GDI+) and then convert it to bitmap/tiff/jpg/png (whatever you can save with GDI+) and save it to the memory. The article will NOT teach you anything useful about image compression, because it uses GDI+ to get the work done. Also please check out great tutorials here on CP on how to get GDI+ to run on your computer before trying this sample. This solution presented here is very simple and easy to understand but unfortunately it is not optimal. I guess the question is why do we need it then? The answer to this question is that sometimes if we are performing an operation in a program every once in a while but not very often, we are rather using simple solution that is not optimal than complicated which is optimal (Obviously, everybody has own preferences). Additionally, this article is intended for beginners, so you do not need to understand absolutely nothing about bitmaps and image compression in order to understand the code (I am not discouraging you from learning it when the time allows).

Background

Honestly I did not use the code extensively. The code was created when I referenced MSDN for some function that I did not use for a while. But then I run across the function CreateStreamOnHGlobal(...) which immediately reminded me of the fact that we can save a GDI+ Bitmap object to a IStream object. And yet again I lost the focus from original project, and before I even knew I was writing the code to verify this fact. In addition I remembered that numerous times when I was reading forums here on CP I run across the question on how to compress an image to memory.

Using the code

Well, as you can see there is not much to it. There is single function that you can copy and paste into your code. But before you can use it, you must link and start up GDI+ in your application. Again, there are nice tutorials on how to do this, here on CP.

// load image

Image image(L"Nice.bmp");

// Create stream with 0 size

IStream* pIStream    = NULL;
if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream) != S_OK)
{
    AfxMessageBox(_T("Failed to create stream on global memory!"));
    return;
}

// Get encoder class id for jpg compression

// for other compressions use

//    image/bmp

//    image/jpeg

//    image/gif

//    image/tiff

//    image/png


CLSID pngClsid;
GetEncoderClsid(L"image/jpeg", &pngClsid);

// Setup encoder parameters

EncoderParameters encoderParameters;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// setup compression level

ULONG quality = 50;
encoderParameters.Parameter[0].Value = &quality;

//  Save the image to the stream

Status SaveStatus = image.Save(pIStream, &pngClsid, &encoderParameters);
if(SaveStatus != Ok)
{
    // this should free global memory used by the stream

    // according to MSDN

    pIStream->Release();
    AfxMessageBox(_T("Failed to save to stream!"));
    return;
}

// get the size of the stream

ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
if(pIStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize) != S_OK)
{
    pIStream->Release();
    AfxMessageBox(_T("Failed to get the size of the stream!"));
    return;
}

// now move the pointer to the beginning of the file

if(pIStream->Seek(lnOffset, STREAM_SEEK_SET, NULL) != S_OK)
{
    pIStream->Release();
    AfxMessageBox(_T("Failed to move the file pointer to "
        "the beginning of the stream!"));
    return;
}

// here you can do what ever you want

/*
    1. You can use global memory
        HGLOBAL hg;
        if(GetHGlobalFromStream(pIStream, &hg) = S_OK)
        ... use hg for something

        2. Copy it into some other buffer
        char *pBuff = new char[ulnSize.QuadPart];

        // Read the stream directly into the buffer
        ULONG ulBytesRead;
        if(pIStream->Read(pBuff, ulnSize.QuadPart, &ulBytesRead) != S_OK)
        {
            pIStream->Release();
            return;
        }
*/

// I am going to save it to the file just so we can 

// load the jpg to a gfx program

CFile fFile;
if(fFile.Open(_T("test.jpg"), CFile::modeCreate | CFile::modeWrite))
{
    char *pBuff = new char[ulnSize.QuadPart];

    // Read the stream directly into the buffer

    ULONG ulBytesRead;
    if(pIStream->Read(pBuff, ulnSize.QuadPart, &ulBytesRead) != S_OK)
    {
        pIStream->Release();
        delete pBuff;
        return;
    }

    fFile.Write(pBuff, ulBytesRead);
    fFile.Close();
    delete pBuff;
}
else AfxMessageBox(_T("Failed to save data to the disk!"));

// Free memory used by the stream

pIStream->Release();

The code is very straight forward. First I load the image, in this case Nice.bmp. Then I have to construct the IStream object which we will use to save our image to. We also need to setup the image encoder. Please refer to MSDN for detailed explanation of encoder parameters as well as GetEncoderClassID function. Then we are ready to save and once we save the data successful we can use the stream, which contains the compressed image just as it would contain on the disk. To prove that I am saving these bytes to the disk, so we can load it into graphics program. The demo code contains nice.bmp, which is compressed by the demo software. The compressed image should be in MemImage sample source code folder.

Conclusion

Please contact me with any questions, suggestions etc. and I will try to answer as soon as I can. Also, English is my third language, so feel free to give me suggestions to improve my writing skills. Please have in mind that I am not a GDI+ expert, nor I am using it very often. Have Fun !

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

About the Author

Amer Gerzic


Originally from Bosnia and Herzegovina, but lived for 6 years in Germany where I did majority of education, then moved to US, where I live since 1999. I like programming, computers in general, but also Basketball, Soccer, Tennis, and many other things. Masters graduate from Grand Valley State University in CIS and working as a full time software developer. Please visit my website www.amergerzic.com
Occupation: President
Company: Infinity Software Solutions, LLC.
Location: United States United States

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
Questionif ULONG quality = 100; a bug is appear!!!!memberhyhhang1:04 16 Jan '08  
GeneralRe: if ULONG quality = 100; a bug is appear!!!!memberAmer Gerzic3:09 16 Jan '08  
GeneralRe: if ULONG quality = 100; a bug is appear!!!!memberhyhhang4:29 16 Jan '08  
GeneralRe: if ULONG quality = 100; a bug is appear!!!!memberAmer Gerzic10:58 17 Jan '08  
GeneralRe: if ULONG quality = 100; a bug is appear!!!!memberXT300021:23 2 Aug '08  
GeneralRe: if ULONG quality = 100; a bug is appear!!!!memberAmer Gerzic2:59 4 Aug '08  
QuestionMemory Image Compression as DLL for AutoIt?memberXT30003:24 13 Sep '07  
AnswerRe: Memory Image Compression as DLL for AutoIt?memberAmer Gerzic3:45 13 Sep '07  
QuestionRe: Memory Image Compression as DLL for AutoIt?memberXT30004:04 13 Sep '07  
AnswerRe: Memory Image Compression as DLL for AutoIt?memberAmer Gerzic5:28 13 Sep '07  
Generalproject built as DLLmemberjmkueter6:45 2 Jan '07  
GeneralRe: project built as DLLmemberAmer Gerzic8:06 2 Jan '07  
GeneralRe: project built as DLLmemberJaneYan17:13 20 Aug '07  
QuestionPlease help me : Facing one problemmemberasdasd@yahoo.com20:30 6 Dec '06  
AnswerRe: Please help me : Facing one problemmemberAmer Gerzic3:16 7 Dec '06  
GeneralRe: Please help me : Facing one problemmemberasdasd@yahoo.com5:09 7 Dec '06  
GeneralRe: Please help me : Facing one problemmemberchandra@codeproject5:16 7 Dec '06  
GeneralRe: Please help me : Facing one problemmemberAmer Gerzic5:45 7 Dec '06  
GeneralRe: Please help me : Facing one problemmemberchandra@codeproject18:26 7 Dec '06  
GeneralRe: Please help me : Facing one problemmemberAmer Gerzic3:09 8 Dec '06  
GeneralThanks, will use this to save disk space!memberAlan Douglas0:42 31 Oct '06  
GeneralRe: Thanks, will use this to save disk space!memberAmer Gerzic3:26 6 Nov '06  
GeneralResolution of ImagememberSumedh Jiwane0:53 17 Jul '06  
GeneralRe: Resolution of ImagememberAmer Gerzic3:13 17 Jul '06  
GeneralRe: Resolution of ImagememberSumedh Jiwane20:38 17 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Aug 2003
Editor: Nishant Sivakumar
Copyright 2003 by Amer Gerzic
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project