<!-- Article image -->
<!-- Add the rest of your HTML here -->
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).
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.
IStream* pIStream = NULL;
if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream) != S_OK)
AfxMessageBox(_T("Failed to create stream on global memory!"));
encoderParameters.Count = 1;
encoderParameters.Parameter.Guid = EncoderQuality;
encoderParameters.Parameter.Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter.NumberOfValues = 1;
ULONG quality = 50;
encoderParameters.Parameter.Value = &quality;
Status SaveStatus = image.Save(pIStream, &pngClsid, &encoderParameters);
if(SaveStatus != Ok)
AfxMessageBox(_T("Failed to save to stream!"));
lnOffset.QuadPart = 0;
if(pIStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize) != S_OK)
AfxMessageBox(_T("Failed to get the size of the stream!"));
if(pIStream->Seek(lnOffset, STREAM_SEEK_SET, NULL) != S_OK)
AfxMessageBox(_T("Failed to move the file pointer to "
"the beginning of the stream!"));
if(fFile.Open(_T("test.jpg"), CFile::modeCreate | CFile::modeWrite))
char *pBuff = new char[ulnSize.QuadPart];
if(pIStream->Read(pBuff, ulnSize.QuadPart, &ulBytesRead) != S_OK)
else AfxMessageBox(_T("Failed to save data to the disk!"));
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.
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 !