Windows Imaging Component - Direct2D C++ - Drawing

2019-06-10 11:00发布

Using Windows Image Component (WIC), I want to do the following for my windows desktop application (Direct2D/C++ with Windows 7 SP1 - Visual Studio 2013)

  1. Choose any type of RenderTarget (Direct2D Hwnd/Bitmap/WICBitmap - etc) for drawing

  2. Create a empty bitmap (D2D1Bitmap or IWICBitmap - whichever applicable)

  3. Begin draw - Fill colour, draw some lines and ellipses - End draw ==> (All in the Bitmap)

  4. At some point of time, I need to save the drawn content in the bitmap as an image in my computer

  5. Place the bitmap in the x1,y1 (top left - xy coordinates) and x2,y2 (bottom right - xy coordinates) of the render target. Because the rest of the space of the window would be used by toolbar.

How do I achieve this using C++/Direct2D?

GDI+ Code for my functionality:

Bitmap* pBmp = NULL; //create null or empty bitmap
Graphics* pGrBuf = NULL; //initialise graphics to null

pBmp = new Bitmap((INT)rectClient.Width, (INT)rectClient.Height);
pGrBuf = new Graphics(pBmp);

On this Graphics, I could always draw Lines, Rectangles, etc..

pGrBuf.DrawRectangle(....)
pGrBuf.DrawLine(...)

In the end, for achieving point number 5

//leave some space (30, 30 in xy co-ordinates) for putting the toolbox in the top
pGrBuf.DrawImage(m_pBmp, 30.0f, 30.0f);

The code for point 4 is intentionally omitted.

1条回答
走好不送
2楼-- · 2019-06-10 11:28

The question have a simple, unambiguous answer, but there are some details that you should (re)consider.

Direct2D is not a panacea framework that will easily outperform others. It's not very clear what are your drawings about and whats their purpose, but there are cases where Direct2D usage is not very appropriate. If you replace GDI(+) with D2D, some of your sufferings will be:

  • (officialy) limited OS support, according to the DirectX version and/or the functions you will use. You will have to forget about Windows XP, (very possibly) Windows Vista and (less possibly) Windows 7
  • the performance (compared to GDI+, GDI) is not always greater. Mainly depends from the way and the purpose you use D2D. There are cases where D2D has very poor performance (usually wrong usage or misunderstood concepts).

But also, the advantages that Direct2D could provide are countless.

Roughly said, Direct2D is nothing but a wrapper around Direct3D. It was introduced with the DirectX 10 and its usage was very similar to GDI(+). But with DirectX 11(1), the Direct2D "principles" were changed and now its more D3D-like. It adds another approaches and drops old ones. It could be a little bit confusing at first. Confusing, also because all the tutorials, articles and whatever D2D resources (including MSDN) in the web are mixed up between the D2D versions. Some of them are for the old version and recommend one thing (approach), other describe the new version.

Anyway, I recommend the new version - ie Direct2D 11.1.

To your question...

  1. The "RenderTarget" is a concept from the "old" D2D. The new one is a DeviceContext
  2. The DeviceContext has a target that could be a D2D1Bitmap(1) - offscreen one, a swap chain's back buffer.
  3. The most typical drawing approach is to call drawing functions within a DeviceContext.BeginScene --- DeviceContext.EndScene block. The drawing functions are very similar to the GDI(+) ones.
  4. There are several ways to do that. You can do it with the help of WIC. Also you can copy your D2D1Bitmap data to a DIBBitmap or you can even (re)draw it over a GDI context.
  5. There is a function DeviceContext.DrawImage, but the way you will do it depends on many things. For example, you could have two bitmaps, that are drawn over two different HWnd (one for the toolbar, another one for the other drawing).

Here are some resources that could help you:

查看更多
登录 后发表回答