The WIC (Windows imaging component) component introduced from Windows Vista processes bitmap in an object-oriented manner and is suitable for use with direct2d.
You can use WIC to save direct2d images as bitmap files.
BelowCodeDemonstrate how to save an image drawn by direct2d with a transparent background color to a PNG bitmap file:
Ccomptr <iwicimagingfactory> spwicimagingfactory;
IFR (direct2dhelper: createwicimagingfactory (& spwicimagingfactory ));
Ccomptr <iwicbitmap> spwicbitmap;
Ccomptr <id2d1rendertarget> spbitmaprt;
Crect RC;
Getclientrect (& rc );
IntWidth = RC. Width (), height = RC. Height ();
IFR (spwicimagingfactory-> createbitmap (
Width,
Height,
// Guid_wicpixelformat32bppbgr,
Guid_wicpixelformat32bpppbgra,
Wicbitmapcacheonload,
& Spwicbitmap ));
D2d1_pixel_format pixelformat = pixelformat (
Dxgi_format_b8g8r8a8_unorm,
D2d1_alpha_mode_premultiplied );
IFR (m_spd2dfactory-> createwicbitmaprendertarget (
Spwicbitmap,
Rendertargetproperties (),
// Rendertargetproperties (d2d1_render_target_type_default, pixelformat ),
& Spbitmaprt ));
Ccomptr <id2d1solidcolorbrush> spsolidbrush;
IFR (spbitmaprt-> createsolidcolorbrush (colgrading: pink), & spsolidbrush ));
// Drawing...
Spbitmaprt-> begindraw ();
Spbitmaprt-> clear (collf: white, 0 ));
D2d1_ellipse ellipse = ellipse (point2f (width/2, height/2), width/2-50, height/2-50 );
Spbitmaprt-> fillellipse (ellipse, spsolidbrush );
Spbitmaprt-> enddraw ();
Ccomptr <iwicstream> spstream;
IFR (spwicimagingfactory-> createstream (& spstream ));
Lpctstr filename = _ T ("Output.png");
IFR (spstream-> initializefromfilename (filename, generic_write ));
Ccomptr <iwicbitmapencoder> spbitmapencoder;
Ccomptr <iwicbitmapframeencode> spframeencode;
IFR (spwicimagingfactory-> createencoder (guid_containerformatpng, null, & spbitmapencoder ));
IFR (spbitmapencoder-> initialize (spstream, wicbitmapencodernocache ));
IFR (spbitmapencoder-> createnewframe (& spframeencode, null ));
IFR (spframeencode-> initialize (null ));
IFR (spframeencode-> setsize (width, height ));
Wicpixelformatguid format = guid_wicpixelformatdontcare;
IFR (spframeencode-> setpixelformat (& format ));
IFR (spframeencode-> writesource (spwicbitmap, null ));
IFR (spframeencode-> commit ());
IFR (spbitmapencoder-> commit ());