How to persist a ID2D1Bitmap ??? ID2D1RenderTarget::DrawBitmap not working
-
Monday, April 02, 2012 12:39 PM
I am attempting to persist a Bitmap extracted from a video accessed using WMF.
I am using ID2D1RenderTarget::DrawBitmap against a ID2D1Bitmap, which is causing ID2D1RenderTarget::EndDraw() to return a failed HRESULT (-2003238894)
Other Draw operations (lines gradient, text) work fine – see SaveAsImageFileSample (http://msdn.microsoft.com/en-us/library/windows/desktop/dd756758(v=vs.85).aspx)
When I try to pass a ID2D1Bitmap (Im using the VideoThumbnailsample: http://msdn.microsoft.com/en-us/library/dd757933(VS.85).aspx) – EndDraw fails.
THe SaveToImageFile function is called as follows:
hr = g_pRT->CreateBitmap(
D2D1::SizeU(m_format.imageWidthPels, m_format.imageHeightPels),
pBitmapData,
pitch,
D2D1::BitmapProperties( // Format = RGB32 D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)),
&pBitmap );
if (hr ==0){SaveToImageFile(&pBitmap);
}
THe SaveToImageFile function is defined as follows:
HRESULT SaveToImageFile(ID2D1Bitmap** bitmap)
{
HRESULT hr = S_OK;
// Create Factories
IWICImagingFactory *pWICFactory = NULL;
ID2D1Factory *pD2DFactory = NULL;
IWICBitmap *pWICBitmap = NULL;
ID2D1RenderTarget *pRT = NULL;
ID2D1GeometrySink *pSink = NULL;
IWICBitmapEncoder *pEncoder = NULL;
IWICBitmapFrameEncode *pFrameEncode = NULL;
IWICStream *pStream = NULL;
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void **>(&pWICFactory)
);
if (SUCCEEDED(hr))
{
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
}
// Create IWICBitmap and RT ICFactory->
static const UINT sc_bitmapWidth = 640;
static const UINT sc_bitmapHeight = 480;
if (SUCCEEDED(hr))
{
hr = pWICFactory->CreateBitmap(
sc_bitmapWidth,
sc_bitmapHeight,
GUID_WICPixelFormat32bppBGR,
WICBitmapCacheOnLoad,
&pWICBitmap
);
}
if (SUCCEEDED(hr))
{
hr = pD2DFactory->CreateWicBitmapRenderTarget(
pWICBitmap,
D2D1::RenderTargetProperties(),
&pRT
);
}
if (SUCCEEDED(hr))
{
// Render into the bitmap
pRT->BeginDraw();
pRT->Clear(D2D1::ColorF(D2D1::ColorF::Green));
D2D1_SIZE_F sourceSize = (*bitmap)->GetSize();
D2D1_SIZE_F targetSize = pRT->GetSize();
D2D1_PIXEL_FORMAT pixelFormat = (*bitmap)->GetPixelFormat();
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties();
props.pixelFormat = pixelFormat;
if (sourceSize.height == targetSize.height && sourceSize.width == targetSize.width)
{ // The following causes EndDraw to fail:
pRT->DrawBitmap((*bitmap), D2D1::RectF(0,0,sourceSize.width, sourceSize.height),(1.0F),D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
}
hr = pRT->EndDraw();
}
if (SUCCEEDED(hr))
{
Going through the Direct2D documentation http://msdn.microsoft.com/en-us/library/windows/desktop/dd370979(v=vs.85).aspx
HRESULT -2003238894 is hex translated into 0x88990012
D2DERR_WRONG_FACTORY - Objects used together were not all created from the same factory instance.
So, How can I persist an ID2D1Bitmap ?
- Edited by JReuben1MVP Monday, April 02, 2012 1:03 PM
All Replies
-
Monday, April 02, 2012 3:48 PMThere is a WinRT sample http://code.msdn.microsoft.com/windowsapps/SaveAsImageFile-68073cb0 Direct2D save to image file - but that wont help - Im on Win7
-
Tuesday, April 03, 2012 2:42 AMModerator
Hello,
Did you mean that you want to save a bitmap in Windows 7 DirectX application? If so, I would suggest you create a new thread in DirectX forum. This forum only talk about the Direct X development in metro app.
Best regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us


