UWP Development Detail records: Loading image files to d2d bitmaps and D3D textures

Source: Internet
Author: User

Loading a file in the UWP typically creates the StorageFile object first, and then calls the Storagefile.openreadasync method to get a Irandomaccessstream interface to read the data:

1 await Storagefile.getfilefromapplicationuriasync (Thenew Uri ("ms-appx:///assets/sample.jpg  ", Urikind.absolute)); 2 await image_file. OpenReadAsync ();

Starting with a Irandomaccessstream stream, the steps to load the image (C + +/CX implementation):

1. Convert irandomaccessstream^ to Isream interface:

1 comptr<istream> source_stream; 2 dx::throwiffailed (:: Createstreamoverrandomaccessstream (Stream, Iid_ppv_args (&source_stream)));

2. Call the Iwicimagingfactory::createdecoderfromstream method (assuming the Iwicimagingfactory object is created) to create a IWICBitmapDecoder object for image decoding, and eventually get The Iwicformatconverter object is converted to a bitmap format suitable for D2D/D3D:

Comptr<iwicbitmapdecoder>decoder;dx::throwiffailed (_device_resources->getwicimagingfactory ()->createdecoderfromstream (Source_stream. Get (), nullptr, Wicdecodemetadatacacheondemand, &decoder)); ComPtr<IWICBitmapFrameDecode>frame;dx::throwiffailed (Decoder->getframe (0, &frame));//Convert the image to a pixel format supported by DIRECT2D. 32bppPBGRA are guaranteed to being supported on all hardware.< /c3>Comptr<iwicformatconverter>covert;dx::throwiffailed (_device_resources->getwicimagingfactory ()->createformatconverter (&covert));D X::throwiffailed (Covert->initialize (frame. Get (), Guid_wicpixelformat32bpppbgra, Wicbitmapdithertypenone, nullptr,0.0f, Wicbitmappalettetypecustom));

3. If the image is for d2d, you can now create the D2D bitmap directly with the Id2d1devicecontext::createbitmapfromwicbitmap method:

Comptr<id2d1bitmap1> d2d_bitmap;dx::throwiffailed (_device_resources->getd2ddevicecontext ()- >createbitmapfromwicbitmap (Covert. Get (), &d2d_bitmap));

4. If you want to use D3D, you need to create the D3D texture first:

//creating D3D Textures for 3D renderingD3d11_texture2d_desc Tex_desc = {0};tex_desc. ArraySize=1; Tex_desc. Bindflags= D3d11_bind_render_target |D3d11_bind_shader_resource;tex_desc. Cpuaccessflags=0; Tex_desc. Format=Dxgi_format_b8g8r8a8_unorm;tex_desc. Height=Height;tex_desc. Width=Width;tex_desc. Miplevels=1; Tex_desc. Miscflags=0; Tex_desc. Sampledesc.count=1; Tex_desc. Sampledesc.quality=0; Tex_desc. Usage=D3d11_usage_default; ComPtr<ID3D11Texture2D>image_texture;dx::throwiffailed (_device_resources->getd3ddevice ()->createtexture2d (&tex_desc, NULL, &image_texture));

5. Get the Idxgisurface interface from the Id3d11texture2d object, D2D and D3D can only interact with DXGI:

Comptr<idxgisurface> dxgi_surface;dx::throwiffailed (image_texture. As (&dxgi_surface));

6. Create the D2D render target with the Id2d1factory::createdxgisurfacerendertarget method:

 float  Dpix = 1.0f  ; float  Dpiy = 1.0f  ;_device_resources ->getd2dfactory ()->getdesktopdpi (&dpix, &< Span style= "color: #000000;"            >DPIY);D 2d1_render_target_properties props  = D2d1::rendertargetproperties (             D2d1_render_target_type_default, D2d1::P ixelformat (Dxgi_format_b8g8r8a8_unorm, d2d1_alpha_mode_premultiplied), Dpix, DPIY); COMPTR  <id2d1rendertarget> render_target;dx::throwiffailed (_device_ Resources ->getd2dfactory ()->createdxgisurfacerendertarget (dxgi_surface. Get (), props, &render_target)); 

7. Create a bitmap using the Id2d1rendertarget::createbitmapfromwicbitmap method (note that you cannot use the bitmap created in step 3rd directly, it is a different render target and the device-dependent bitmap is not generic):

Comptr<id2d1bitmap> bitmap;dx::throwiffailed (render_target->createbitmapfromwicbitmap (Covert. Get (), &bitmap));

8. Draw the bitmap to the render target, which is actually drawn to the D3D texture:

Render_target->BeginDraw (); Render_target-drawbitmap (bitmap. Get ()); Render_target->enddraw ();

Reference: MSDN Overview of DIRECT2D and Direct3D interoperability

UWP Development Detail records: Loading image files to d2d bitmaps and D3D textures

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.