Save CBitmap to File

It has always astounded me why the CBitmap class never implemented saving to a file.  Here's a nice and tidy way:

#include <atlimage.h>

#include <Gdiplusimaging.h>


        CBitmap bitmap;

        bitmap.CreateBitmap(width, height, 1, 32, rgbData);

        CImage image;


        image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatBMP);

8 thoughts on “Save CBitmap to File

  1. Dear Sir,

    This is Farzad Ebrahimi. I’m Visual C++6.0 Programmer. I want to use your solution for Save CBitmap to File. But as I know “atlimage.h” and “Gdiplusimaging.h” libraries are found in visual studio 2002 and I haven’t it.

    Please guide me how can I use your solution with Visual Studio6.0.

    Thank you in advance for any information you can give me and thank you for your early reply.

    Best Regards,

    Farzad Ebrahimi

  2. atlimage.h was added post-v6 and implements (amongst other things) the CImage class.

    CImage wraps a combination of Win32 and GDI+ functionality. My usage in this context is strictly for the Save method. The CImage::Save method basically wraps the Gdiplus::Bitmap::Save method. It also utilizes the Gdiplus::GetImageEncoders (and dependant methods) to get the right encoder. has a good example of saving a bitmap to file using GDI+ directly (i.e. without ATL).

  3. This is also the way i do it. But on NT4, the always returns E_FAIL and i haven’t found out why that happens. Do you happen to know any way to use this under NT4 or why it fails?

  4. Hi Sir, I am a C++ Developer. We have used save api of gdi plus to compress an image. It works well but it doesn’t allow us to compress any image twice. IS there any way through which progressive compression of the same image can be done ?

  5. hi… using this 6.0
    CBitmap bitmap;

    bitmap.CreateBitmap(width, height, 1, 32, rgbData);

    CImage image;


    image.Save(_T(“C:\\test.bmp”), Gdiplus::ImageFormatBMP);

    but i have error:

    error C2664: ‘Attach’ : cannot convert parameter 1 from ‘class CBitmap’ to ‘const class CImage &’
    Reason: cannot convert from ‘class CBitmap’ to ‘const class CImage’
    No constructor could take the source type, or constructor overload resolution was ambiguous
    D:\Wrk\sample1\sample1View.cpp(297) : error C2664: ‘int __thiscall CImage::Save(const char *,enum cximage_type)’ : cannot convert parameter 2 from ‘const struct _GUID’ to ‘enum cximage_type’
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    plz help me……thanks…

  6. CBitmap cannot direct attach into CImage..
    have to convert into HBITMAP first..


Leave a Reply

Your email address will not be published. Required fields are marked *