Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. D2D/GDI+ Interop Problem

D2D/GDI+ Interop Problem

Scheduled Pinned Locked Moved Graphics
comgraphicshelpvisual-studiowinforms
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11678350
    wrote on last edited by
    #1

    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);
    
    L 1 Reply Last reply
    0
    • U User 11678350

      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);
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are trying to do this in a console application. I do not think that will work since you have no Window to draw on. You need to create a Windows app to do it.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups