Jump to content
  • Advertisement
Sign in to follow this  
Waterfox

[D2D] Share/convert bitmap among software and hardware rendertarget.

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a hardware compatible rendertarget that contains an ID2D1Bitmap that I'd like to save to an image. Unfortunately WIC rendertargets are software only, so I'm wondering is there someway I can share or copy the data from the hardware bitmap to a software one for the WIC rendertarget? Or even better, convert that hardware bitmap directly into an IWICBitmap to save to an image.

Share this post


Link to post
Share on other sites
Advertisement
Well I've managed to achieve this by create a WICBitmap from a HBITMAP from the DC of the Direct2D window. It works but if there's a better way please say.
void Canvas::ExportFrame()
{
int width=rc.right, height=rc.bottom;
IWICBitmapEncoder *pEncoder = NULL;
IWICBitmapFrameEncode *pFrameEncode = NULL;
IWICStream *pStream = NULL;
WICFactory->CreateStream(&pStream);
IWICBitmap *WICBitmap;
wchar_t ext[MAX_PATH];
PAINTSTRUCT ps;
HDC main = GetDC(hwnd);
HDC temp=CreateCompatibleDC(main);
HDC mem=CreateCompatibleDC(main);
HBITMAP bm=CreateCompatibleBitmap(main, width, height);
HBITMAP bm2=CreateCompatibleBitmap(main, width, height);
SelectObject(mem, bm);
SelectObject(temp, bm2);

playing=true;
BeginPaint(hwnd, &ps);
BitBlt(temp,0,0,width,height,main,0,0,SRCCOPY);
canvas.Draw();
BitBlt(mem,0,0,width,height,main,0,0,SRCCOPY);
BitBlt(main,0,0,width,height,temp,0,0,SRCCOPY);
EndPaint(hwnd, &ps);
playing=false;
ReleaseDC(hwnd, main);
DeleteDC(temp);
DeleteDC(mem);
DeleteObject(bm2);

WICFactory->CreateBitmapFromHBITMAP(bm, 0, WICBitmapUseAlpha, &WICBitmap);
DeleteObject(bm);

for(int i = FileExtension; OpenFileName; i++) ext[i-FileExtension]=OpenFileName;
WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
WICPixelFormatGUID type = GUID_ContainerFormatGif;

if((!_wcsicmp(ext, L"jpg"))||(!wcscmp(ext, L"jpeg")))type = GUID_ContainerFormatJpeg;
else if(!_wcsicmp(ext, L"gif"))type = GUID_ContainerFormatGif;
else if(!_wcsicmp(ext, L"png"))type = GUID_ContainerFormatPng;
else if(!_wcsicmp(ext, L"bmp"))type = GUID_ContainerFormatBmp;

pStream->InitializeFromFilename(OpenFileName, GENERIC_WRITE);
WICFactory->CreateEncoder(type, NULL, &pEncoder);
pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
pEncoder->CreateNewFrame(&pFrameEncode, NULL);
pFrameEncode->Initialize(NULL);
pFrameEncode->SetSize(width, height);
pFrameEncode->SetPixelFormat(0);
pFrameEncode->WriteSource(WICBitmap, NULL);
pFrameEncode->Commit();
pEncoder->Commit();
SafeRelease(&pEncoder);
SafeRelease(&pFrameEncode);
SafeRelease(&pStream);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!