Windows成像组件 - Direct2D C ++ - 绘图,保存 [英] Windows Imaging Component - Direct2D C++ - Drawing, Saving

查看:596
本文介绍了Windows成像组件 - Direct2D C ++ - 绘图,保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Windows映像组件(WIC),我想为我的Windows桌面应用程序(Direct2D / C ++与Windows 7 SP1 - Visual Studio 2013)执行以下操作



<
  • 选择任何类型的RenderTarget(Direct2D Hwnd / Bitmap / WICBitmap -
    等)绘制


  • 空白位图(D2D1Bitmap或IWICBitmap -
    ,视适用情况而定)


  • 开始绘制 - 填充颜色,绘制一些直线和椭圆 -
    draw ==>(在位图中的所有)


  • 在某个时间点,我需要
    将绘制的内容保存为位图


  • 将位图放在x1,y1(左上角 - xy坐标)和x2,y2
    (右下角 - xy坐标)。因为窗口空间的
    剩余部分将被工具栏使用。


  • 这使用C ++ / Direct2D?



    我的功能的GDI +代码

      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);

    在这个图形上,我总是可以绘制线条,矩形等。

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

    到达点数5

      //留下一些空间(在xy坐标中为30,30),用于将工具箱置于顶部
    pGrBuf.DrawImage(m_pBmp,30.0f,30.0f);

    点4的代码有意省略。

    解决方案

    这个问题有一个简单,明确的答案,但有一些细节,你应该(重新考虑)。



    Direct2D不是一个万灵药框架,将容易优于其他。它不是很清楚你的图纸是什么,什么是他们的目的,但有些情况下Direct2D使用不是很适当。如果你用D2D替换GDI(+),你的一些苦难将是:




      <版本和/或您将使用的功能。你将不得不忘记Windows XP,(非常可能)Windows Vista和(可能)Windows 7
    • 的性能(与GDI +,GDI相比)并不总是更大。主要取决于使用D2D的方式和目的。有些情况下,D2D的性能非常差(通常是错误的使用或误解的概念)。



    但是,Direct2D可以提供的优势是无数的。



    粗略地说,Direct2D只是一个Direct3D的包装器。它是与DirectX 10一起引入的,其用法非常类似于GDI(+)。但是使用DirectX 11(1),Direct2D的原理改变了,现在它更像D3D的样子。它增加了另一种方法,并删除旧的。这可能有点混乱起初。混乱,也因为所有的教程,文章和任何D2D资源(包括MSDN)在网络中混合在D2D版本之间。其中一些是旧版本,推荐一个东西(方法),其他描述新版本。



    无论如何,我推荐新版本 - 即Direct2D 11.1。 / p>

    您的问题...


    1. RenderTarget 老D2D。新的一个是DeviceContext

    2. DeviceContext有一个目标,可以是一个D2D1Bitmap(1) - offscreen,一个交换链的后台缓冲区。

    3. 最典型的绘图方法是在DeviceContext.BeginScene --- DeviceContext.EndScene块中调用绘图函数。绘图功能与GDI(+)非常相似。

    4. 有几种方法可以做到。你可以在WIC的帮助下做到这一点。还可以将D2D1Bitmap数据复制到DIBBitmap,或者甚至可以通过GDI上下文来绘制它。

    5. 有一个函数DeviceContext.DrawImage,但是你会这样做取决于许多事情。例如,您可以有两个位图,它们通过两个不同的HWnd(一个用于工具栏,另一个用于另一个图形)绘制。

    以下是一些可以帮助您的资源:




    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.

    解决方案

    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:

    这篇关于Windows成像组件 - Direct2D C ++ - 绘图,保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆