locked
How to load ID2D1Bitmap1 from Assets

    Question

  • Hi,

    Hi,

    I'm new to Direct2D and Win8 Apps and there's a simple question:

    How do I load an Asset (PNG) to a D2D Bitmap for drawing it?

    I assume, there must be a shortcut to get an ID2D1Bitmap1 from a BitmapImage ... ?

    BitmapImage^ bitmapImage = ref new BitmapImage(ref new Uri("ms-appx:/Assets/ball.png"));

    Thanks in advance,

    Roland

    Monday, December 17, 2012 10:29 AM

Answers

  • Hi,

    You should load the bitmap from WIC and create a D2D image from D2D context. Please follow these codes

        ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
        DX::ThrowIfFailed(
            m_wicFactory->CreateDecoderFromFilename(
                L"windowsbig-sdk.png",
                nullptr,
                GENERIC_READ,
                WICDecodeMetadataCacheOnDemand,
                &wicBitmapDecoder
                )
            );
    
        ComPtr<IWICBitmapFrameDecode> wicBitmapFrame;
        DX::ThrowIfFailed(
            wicBitmapDecoder->GetFrame(0, &wicBitmapFrame)
            );
    
        ComPtr<IWICFormatConverter> wicFormatConverter;
        DX::ThrowIfFailed(
            m_wicFactory->CreateFormatConverter(&wicFormatConverter)
            );
    
        DX::ThrowIfFailed(
            wicFormatConverter->Initialize(
                wicBitmapFrame.Get(),
                GUID_WICPixelFormat32bppPBGRA,
                WICBitmapDitherTypeNone,
                nullptr,
                0.0,
                WICBitmapPaletteTypeCustom  // the BGRA format has no palette so this value is ignored
                )
            );
    
        double dpiX = 96.0f;
        double dpiY = 96.0f;
        DX::ThrowIfFailed(
            wicFormatConverter->GetResolution(&dpiX, &dpiY)
            );
    
        DX::ThrowIfFailed(
            m_d2dContext->CreateBitmapFromWicBitmap(
                wicFormatConverter.Get(),
                BitmapProperties(
                    PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
                    static_cast<float>(dpiX),
                    static_cast<float>(dpiY)
                    ),
                &m_logoBitmap  //D2Dbitmap
                )
            );

    You can get the whole codes from
    http://code.msdn.microsoft.com/windowsapps/Direct2D-custom-image-7a1f2cb5

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 19, 2012 2:56 AM
    Moderator