Using Direct2D for Rendering WIC Bitmap Sources

Once having a WIC (Windows Imaging Component) source, we can render it by using GDI, GDI+ or Direct2D. There are many examples over the internet including MSDN Library that describes how to perform this.
This article describes how to use WIC Wrapper Library, which is designed for using WIC without worrying about directly deal with COM interfaces. The sample code as well as attached demo project uses MFC, but WIC Wrapper Library can be used in ATL or raw-Win32 projects, as well.

Let’s see the code, step by step!

  1. Add WIC and Direc2D factoy members to the aplication class.
  2. Create WIC and Direc2D factory instances after initializing OLE libraries.
  3. Release factories befor calling AfxOleTerm.

     
  4. Add WIC bitmap source member to document class then override CDocument::OnOpenDocument and CDocument::DeleteContents. Also add an implementation function that loads the WIC bitmap source from a file.


    Now we have a WIC bitmap source in the document class that will be used in the view class to create a Direct2D bitmap and render it in the view’s window.
  5. Add Direct2D render target and bitmap to the view class
  6. Create a Direct2D render target for the view window.

    A good place to call _CreateDirect2DRenderTarget is in the WM_CREATE message handler.
  7. Resize the Direct2D render target each time is necessary.

    If the render target must fit the view window, then the place to call _Resize2DRenderTarget is the WM_SIZE mesage handler.
  8. Create the Direct2D bitmap from WIC bitmap.

    In our case, the best place to call _CreateDirect2DBitmapFromWICBitmap is the overridden CView::OnUpdate function.
  9. Finally, render the bitmap.

    Of course, in a class derived from CView, the place for calling _RenderD2DBitmap is the overridden CView::OnDraw virtual function.
  10. Remains just few little things to ajust: handle WM_ERASEBKGND and override CScrollView::OnScrollBy. Anyway, you can find all the implementation details in the attached demo application.

Demo application

The demo application is a basic image file viewer that uses WIC Wrapper Library.
Download: WIC Wrapper Library v2_0 and Image Viewer Demo Project.zip (1281) (full WIC Wrapper Library source code is included).

Demo Image Viewer

Demo Image Viewer

Requirements

Resources, related articles, examples and projects

3 thoughts on “Using Direct2D for Rendering WIC Bitmap Sources

  1. Gary Gray

    I tried the demo project .. the WICWrapperLibrary compiles ok .. but the DemoImageViewer gives a ton of com errors like:

    wic wrapper library v2.0\_interface\wic\wic_bitmapsource.h(44): error C2664: ‘ns::com::Base::Base(IUnknown *)’ : cannot convert parameter 1 from ‘ns::wic::IWICBitmapSource *’ to ‘IUnknown *’

    What wrong with the solution ? I’m calling out SDK 7.1 in the Platform Toolkit

    Reply
      1. Gary Gray

        your right .. at work it builds fine, I don’t know whats wrong with my Win7 64bit at home, Work is Win10 64bit. The reason I apparently have to use WIC is I need to read/write jpeg metadata (i was storing info in the comment using LeadTools now I want to go 64bit and get rid of LeadTools) GDI+ doesn’t seem to be able to write an 8bit Greyscale JPEG. I’ve got it all to work with WIC now, but this new CD2DBitmap could be very useful.

        Thanks for responding
        Gary

        Reply

Leave a Reply