4

How to save ID2D1Bitmap to PNG file did not help me.

I'm working with Direct2D.

I want to save a ID2D1**Hwnd**RenderTarget *m_pRenderTarget to file as bmp or png etc.

But the sample I found on MSDN use ID2D1RenderTarget.

In my case I drawn my figure in m_pRenderTarget, I used the method ID2D1Bitmap::CopyFromRenderTarget (...) to get an ID2D1Bitmap.

After that, to use the saving function bellow, I have to convert the ID2D1Bitmap I got to IWICBitmap. Because the function bellow doesn't use ID2D1Bitmap ...

I did not found yet how to do this.

thanks.

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 a Direct2D code and my drawing image is hold on my ID2D1HwndRenderTarget. I want to save the drawn image hold by my ID2D1HwndRenderTarget on disk.

the sample I found create 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 IWICBitmap... to write it. someone have a clue with how to bind ID2D1HwndRenderTarget and IWICBitmap

Community
  • 1
  • 1
hughes
  • 177
  • 1
  • 5
  • 11
  • `ID2D1HwndRenderTarget` just inherits from the `ID2D1RenderTarget`. So why didn't the sample help? Could you also post the link of the sample? – sraok Jan 26 '13 at 18:09
  • Hi, Here is a link for the code [link](http://code.msdn.microsoft.com/d2dsaveasimage/Release/ProjectReleases.aspx?ReleaseId=3071) The sample didn't help because I used CComPtr m_fabriqueD2D1; ID2D1HwndRenderTarget *m_pRenderTarget; – hughes Jan 26 '13 at 19:35
  • Have you tried CreateBitmapFromWicBitmap then ID2D1Bitmap::CopyFromRenderTarget? – vt. May 11 '18 at 14:42

0 Answers0