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.Attach(bitmap);

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

Published Monday, September 25, 2006 9:48 AM by PeterRitchie

Comments

# re: Save CBitmap to File

Thanks a lot! I wonder why I can't find something like that on MSDN. Maybe it's too simple.

Friday, November 10, 2006 3:57 PM by Ingo

# re: Save CBitmap to File

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

email: farzadebr@yahoo.com

Wednesday, December 13, 2006 3:29 AM by Farzad Ebrahimi

# @Farzad re: VC++ 6.0

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.  http://www.codeguru.com/cpp/g-m/gdi/gdi/article.php/c3661/ has a good example of saving a bitmap to file using GDI+ directly (i.e. without ATL).

Thursday, January 11, 2007 12:03 PM by PeterRitchie

# re: Save CBitmap to File

This is also the way i do it. But on NT4, the img.save(..)-function 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?

Wednesday, June 20, 2007 3:02 AM by Thomas

Leave a Comment

(required) 
(required) 
(optional)
(required)