D2D/GDI+ Interop Problem
-
I have recently read an article about Direct2D and GDI Interop from MSDN and I just wanted to make an attempt on that. The problem with my code is that there is no (D2D) red rectangles at the output file but it does not produce any compile-time or runtime errors. That really makes me frustrated as I have already spent my valuable weekend for that without any progress. :(( Any help is appreciated.:rose: Ref. article: Direct2D and GDI Interoperability Overview (Windows)[^] Code block:
#include
#include
#include
#include#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "GdiPlus.lib")
#pragma comment(lib, "Windowscodecs.lib")using namespace std;
void main()
{
HRESULT hr;
ID2D1Factory* factory;
ID2D1DCRenderTarget* target;
ID2D1SolidColorBrush* brush;D2D1CreateFactory(D2D1\_FACTORY\_TYPE\_SINGLE\_THREADED, &factory); // initialize GDI+ ULONG\_PTR token; Gdiplus::GdiplusStartup(&token, new Gdiplus::GdiplusStartupInput(), nullptr); auto image = new Gdiplus::Image(L"C:\\\\temp\\\\test.jpg"); auto g = Gdiplus::Graphics::FromImage(image); auto prop = D2D1\_RENDER\_TARGET\_PROPERTIES{ D2D1\_RENDER\_TARGET\_TYPE\_HARDWARE , D2D1\_PIXEL\_FORMAT{ DXGI\_FORMAT\_B8G8R8A8\_UNORM , D2D1\_ALPHA\_MODE\_PREMULTIPLIED }, 144.0f, 144.0f, D2D1\_RENDER\_TARGET\_USAGE\_GDI\_COMPATIBLE, D2D1\_FEATURE\_LEVEL\_DEFAULT }; hr = factory->CreateDCRenderTarget(&prop, &target); auto rect = RECT{ 0L,0L,800L, 600L }; cout << boolalpha << (image == nullptr) << boolalpha << (g == nullptr) << endl; HDC deviceContext = g->GetHDC(); // binds D2D device context to GDI+ device context. target->BindDC(deviceContext, &rect); // DrawRectangle using D2d target->BeginDraw(); target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red, 1.0f), &brush); target->DrawRectangle(D2D1\_RECT\_F{ 5.0f,5.0f,10.0f,10.0f }, brush); target->DrawRectangle(D2D1\_RECT\_F{ 10.0f,10.0f,30.0f,30.0f }, brush); hr = target->EndDraw(); cout << boolalpha << SUCCEEDED(hr) << endl; CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3,{ 0x9a,0x73,0x00,0x00,0xf8,0x1e,0xf3,0x2e } }; // Transcode and save as PNG image file image->Save(L"C:\\\\temp\\\\output.png", &pngEncoderClsId);
-
I have recently read an article about Direct2D and GDI Interop from MSDN and I just wanted to make an attempt on that. The problem with my code is that there is no (D2D) red rectangles at the output file but it does not produce any compile-time or runtime errors. That really makes me frustrated as I have already spent my valuable weekend for that without any progress. :(( Any help is appreciated.:rose: Ref. article: Direct2D and GDI Interoperability Overview (Windows)[^] Code block:
#include
#include
#include
#include#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "GdiPlus.lib")
#pragma comment(lib, "Windowscodecs.lib")using namespace std;
void main()
{
HRESULT hr;
ID2D1Factory* factory;
ID2D1DCRenderTarget* target;
ID2D1SolidColorBrush* brush;D2D1CreateFactory(D2D1\_FACTORY\_TYPE\_SINGLE\_THREADED, &factory); // initialize GDI+ ULONG\_PTR token; Gdiplus::GdiplusStartup(&token, new Gdiplus::GdiplusStartupInput(), nullptr); auto image = new Gdiplus::Image(L"C:\\\\temp\\\\test.jpg"); auto g = Gdiplus::Graphics::FromImage(image); auto prop = D2D1\_RENDER\_TARGET\_PROPERTIES{ D2D1\_RENDER\_TARGET\_TYPE\_HARDWARE , D2D1\_PIXEL\_FORMAT{ DXGI\_FORMAT\_B8G8R8A8\_UNORM , D2D1\_ALPHA\_MODE\_PREMULTIPLIED }, 144.0f, 144.0f, D2D1\_RENDER\_TARGET\_USAGE\_GDI\_COMPATIBLE, D2D1\_FEATURE\_LEVEL\_DEFAULT }; hr = factory->CreateDCRenderTarget(&prop, &target); auto rect = RECT{ 0L,0L,800L, 600L }; cout << boolalpha << (image == nullptr) << boolalpha << (g == nullptr) << endl; HDC deviceContext = g->GetHDC(); // binds D2D device context to GDI+ device context. target->BindDC(deviceContext, &rect); // DrawRectangle using D2d target->BeginDraw(); target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red, 1.0f), &brush); target->DrawRectangle(D2D1\_RECT\_F{ 5.0f,5.0f,10.0f,10.0f }, brush); target->DrawRectangle(D2D1\_RECT\_F{ 10.0f,10.0f,30.0f,30.0f }, brush); hr = target->EndDraw(); cout << boolalpha << SUCCEEDED(hr) << endl; CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3,{ 0x9a,0x73,0x00,0x00,0xf8,0x1e,0xf3,0x2e } }; // Transcode and save as PNG image file image->Save(L"C:\\\\temp\\\\output.png", &pngEncoderClsId);