How to save bitmap to file using direct2d and ID2D1HwndRenderTarget

How to save ID2D1Bitmap file to PNG didn't work for me.

I am working with Direct2D.

I want to save ID2D1**Hwnd**RenderTarget *m_pRenderTarget

to file as bmp or png etc.

But the sample I found on MSDN uses ID2D1RenderTarget

.

In my case, I drew my shape in m_pRenderTarget, I used the method ID2D1Bitmap::CopyFromRenderTarget (...)

to get ID2D1Bitmap.

After that, in order to use the save function below I need to transform ID2D1Bitmap

I got IWICBitmap

. Since the function below does not use ID2D1Bitmap

...

I haven't found how to do this yet.

thank.

if (SUCCEEDED(hr))
{

    //
    // Save image to file
    //

    hr = pWICFactory->CreateStream(&pStream);
}

WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
if (SUCCEEDED(hr))
{
    static const WCHAR filename[] = L"output.png";
    hr = pStream->InitializeFromFilename(filename, GENERIC_WRITE);
}
if (SUCCEEDED(hr))
{
    hr = pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
}
if (SUCCEEDED(hr))
{
    hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
}
if (SUCCEEDED(hr))
{
    hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
}
if (SUCCEEDED(hr))
{
    hr = pFrameEncode->Initialize(NULL);
}
if (SUCCEEDED(hr))
{
    hr = pFrameEncode->SetSize(sc_bitmapWidth, sc_bitmapHeight);
}
if (SUCCEEDED(hr))
{
    hr = pFrameEncode->SetPixelFormat(&format);
}
if (SUCCEEDED(hr))
{
    hr = pFrameEncode->WriteSource(pWICBitmap, NULL);
}
if (SUCCEEDED(hr))
{
    hr = pFrameEncode->Commit();
}
if (SUCCEEDED(hr))
{
    hr = pEncoder->Commit();
}

      

to explain easily, I already had Direct2D code and my picture image is being held on my ID2D1HwndRenderTarget. I want to save the captured image to my ID2D1HwndRenderTarget on disk.

the found sample creates a new IWICBitmap and with        pD2DFactory->CreateWicBitmapRenderTarget(pWICBitmap,D2D1::RenderTargetProperties(),&pRT);

create a new ID2D1RenderTarget to draw. After drawing operations, it wrote the bitmap image 
    hr = pFrameEncode->WriteSource(pWICBitmap, NULL);

      

In my case, I don't know how to put the image from my ID2D1HwndRenderTarget to the IWICBitmap ... in order to write it. someone knows how to link ID2D1HwndRenderTarget and IWICBitmap

+3


source to share





All Articles