Use WIC to save the drawing drawn by direct2d as a file

Source: Internet
Author: User

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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.