目前博客园中成系列的Direct2D的教程有

1、万一的 Direct2D 系列,用的是Delphi 2009

2、zdd的 Direct2D 系列,用的是VS中的C++

3、本文所在的 Direct2D教程 系列,用的是VS2010的Visual Basic语言(可以很方便的转为C#),基于Windows API Code Pack 1.1。

还有官方的说明文档 Direct2D ,用的是C++。

本系列的前几篇文章:

Direct2D教程I——简介及首个例子

Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解

Direct2D教程III——几何(Geometry)对象

Direct2D教程IV——笔刷(Brush)对象

Direct2D教程V——位图(Bitmap)和位图笔刷(BitmapBrush)

Direct2D教程VI——转换(Transform)

Direct2D教程VII——变换几何(TransformedGeometry)对象

研究Direct2D已经有一段时间了。在参阅了一些相关的文章后,发现虽然Windows API Code Pack 1.1对Direct2D封装的比较好,但也有不足的地方(封装不完全)。

1、在WIC组件中,没有封装BitmapFrameEncode对象

在前文中曾介绍,WIC组件中有BitmapFrameDecode对象,负责图像数据的解码。相对应的也应该有BitmapFrameEncode对象,负责图像数据的编码。但是在Windows API Code Pack 1.1中的WIC组件中没有这个对象,仅仅在ImagingBitmap对象中提供了SaveToFile方法。该方法提供了图像数据保存到文件的途径,但是扩展性不够,没有提供不同格式的参数设置(全采用该格式的默认设置)

2、没有提供ID2D1Effect对象的封装

Direct2D提供了一些图像的特效,通过ID2D1Effect对象实现,参看 Built-in Effects 。由于没有封装ID2D1Effect对象,自然也就没法实现图像的特效了。

期待Windows API Code Pack的下个版本能弥补上面的两个不足

言归正传,接下来介绍几何(Geometry)对象的运算。

几何(Geometry)对象的运算

在前文 Direct2D教程III——几何(Geometry)对象 中介绍了几何(Geometry)对象的一些高级功能。其中一个就是两个几何(Geometry)对象的运算功能。

运算方法的原型定义

 
Public Sub CombineWithGeometry(inputGeometry As Direct2D1.Geometry, combineMode As Direct2D1.CombineMode, geometrySink As Direct2D1.ISimplifiedGeometrySink)
Public Sub CombineWithGeometry(inputGeometry As Direct2D1.Geometry, combineMode As Direct2D1.CombineMode, geometrySink As Direct2D1.ISimplifiedGeometrySink, flatteningTolerance As Single)
Public Sub CombineWithGeometry(inputGeometry As Direct2D1.Geometry, combineMode As Direct2D1.CombineMode, geometrySink As Direct2D1.ISimplifiedGeometrySink, flatteningTolerance As Single, inputGeometryTransform As Direct2D1.Matrix3x2F)
Public Enum CombineMode
    Union = 0
    Intersect = 1
    Xor = 2
    Exclude = 3
End Enum 

该方法的几个参数的意义如下:

inputGeometry:要和本对象运算的几何对象,类型是Geometry类

combineMode:运算的类型,类型是CombineMode枚举,分别是Union(并集)、Intersect(交集)、Xor(异或,两个对象不属于对方的部分的合集)、Exclude(差集)

geometrySink:运算的结果要添加到的Sink对象,类型是ISimplifiedGeometry接口

flatteningTolerance:运算结果的精度,类型是数值,表示模拟结果的每条边的长度,值越小越精细,在可以接受的精度下,可以适当提高精度的数值

inputGeometryTransform:和本对象运算的几何对象要进行的转换(Transform),类型是Matrix3x2F,是先转换再运算

下面是个示例代码,用两个矩形对象来演示运算结果,运算的类型我们用Xor表示,其余的和这个类似

 
Public Class clsDirect2DSample17
    Inherits clsDirect2DSample11

Public Shadows Sub Render()
        If Not _renderTarget Is Nothing Then
            With _renderTarget
                .BeginDraw()

.Clear(New Direct2D1.ColorF(Color.Chocolate.ToArgb))

Dim BorderBrush As Direct2D1.SolidColorBrush = _renderTarget.CreateSolidColorBrush(New Direct2D1.ColorF(0, 0, 0))
                Dim Bmp As Direct2D1.D2DBitmap = LoadBitmapFromFile("216.png")
                Dim BmpBrush As Direct2D1.BitmapBrush = _renderTarget.CreateBitmapBrush(Bmp)

Dim R1 As Direct2D1.RectangleGeometry, R2 As Direct2D1.RectangleGeometry
                R1 = _d2DFactory.CreateRectangleGeometry(New Direct2D1.RectF(30, 30, 150, 150))
                R2 = _d2DFactory.CreateRectangleGeometry(New Direct2D1.RectF(60, 60, 180, 180))

Dim P As Direct2D1.PathGeometry
                Dim Sink As Direct2D1.GeometrySink

P = _d2DFactory.CreatePathGeometry()
                Sink = P.Open

R1.CombineWithGeometry(R2, Direct2D1.CombineMode.Xor, Sink)

Sink.Close()

.DrawGeometry(P, BorderBrush, 3)
                .FillGeometry(P, BmpBrush)

.EndDraw()
            End With
        End If
    End Sub
End Class

下面是该示例代码的效果图

故事到此似乎很完美,然而,接下来的调试彻底让我崩溃了,先看看效果图

代码和前面的代码几乎一样,仅仅是用EllipseGeometry对象替换了RectangleGeometry对象,结果出现了不可预知的错误,大大出乎我的意料。在反复检查代码后,证实不是代码的写错后,我开始怀疑Windows API Code Pack 1.1来。

RectangleGeometry对象有一个Rectangle属性,其还有Top、Left、Right、Bottom四个属性,在上文的示例代码中,这四个值分别是30、30、150、150。

同样EllipseGeometry对象有一个Ellipse属性,其有Point属性,其还有X、Y两个属性,在调试代码时发现,这两个值是0、0。这太奇怪了,正常值是100、100(我设的中心点是(100,100))。这也许是Windows API Code Pack 1.1封装EllipseGeometry对象的一个失误。导致这两个值异常,导致几何对象运算出现不可预知的错误。

遗憾的是,在几个几何对象中(RectangleGeometry、EllipseGeometry、RoundedRectangleGeometry、PathGeometry)仅仅有RectangleGeometry对象支持CombineWithGeometry方法,其余的都会出现不可预知的错误

我很喜欢Windows API Code Pack 1.1,它是微软推出的,兼容性应该是最好的。

在测试CombineWithGeometry方法后,大失所望。没有详细的测试后,就推出了。期待Windows API Code Pack 的下一个版本,本系列的教程就到此为止了。

听闻SharpDx对Direct2D封装的很好,下图是SharpDx官网的说明图

可以看出SharpDx的优势了,打算研究一段时间。再视情况而定是否写新的教程系列。

也欢迎网友提供相关的教程,谢谢!!!!!

Direct2D教程VIII——几何(Geometry)对象的运算,本系列的终结篇的更多相关文章

  1. Direct2D教程III——几何(Geometry)对象

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  2. SharpDX之Direct2D教程I——简单示例和Color(颜色)

    研究Direct2D已经有一段时间了,也写了一个系列的文章 Direct2D ,是基于Windows API Code Pack 1.1.在前文 Direct2D教程VIII——几何(Geometry ...

  3. Direct2D教程VII——变换几何(TransformedGeometry)对象

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  4. Direct2D教程VI——转换(Transform)

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  5. Direct2D教程V——位图(Bitmap)和位图笔刷(BitmapBrush)

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  6. Direct2D教程IV——笔刷(Brush)对象

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  7. SharpDX之Direct2D教程II——加载位图文件和保存位图文件

    本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...

  8. Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解

    目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一的Direct2D系列和zdd的Direct2D系列.有兴趣的网友可以去看看.本系列也是介绍Direct2D的教程,是基 ...

  9. java程序操作Geometry对象

    Geometry 空间地理对象,Oracle中存储Geometry对象的字段类型是 MDSYS.SDO_GEOMETRY,在数据库中构建Geometry对象的方法: v_pointarray MDSY ...

随机推荐

  1. CRC 概述

    Acquired from: ftp.adelaide.edu.au:/pub/rocksoft/crc_v3.txt or ftp://ftp.rocksoft.com/papers/crc_v3. ...

  2. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  3. 理解 process.initgroups(user, extra_group)

    这个函数是对 linux  C函数 initgroups()  的包装 node.js 官方文档非常含糊,还是看 linux C函数文档的解释!非常清楚明确. The initgroups() fun ...

  4. Linux虚拟主机管理系统---wdcp

    关于WDCP这款虚拟主机管理系统,是疯子使用的第二款Linux虚拟主机管理系统,使用是挺简单的,以前好像是因为编码问题而放弃这款面板. WDCP功能比较完善,基本上需要的功能都能满足,例如:在线下载. ...

  5. Revit API找到风管穿过的墙(当前文档和链接文档)

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class c ...

  6. c++ #ifdef的用法

    http://www.tuicool.com/articles/mIJnumB #ifdef的用法 灵活使用#ifdef指示符,我们可以区隔一些与特定头文件.程序库和其他文件版本有关的代码.代码举例: ...

  7. .NET:默认是按值传递的

    小测试 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  8. WebRTC 基于GCC的拥塞控制(下)

    转自;http://blog.csdn.net/ljh081231/article/details/79152578 本文在文章[1]的基础上,从源代码实现角度对WebRTC的GCC算法进行分析.主要 ...

  9. 常见排序的JAVA实现和性能测试

    五种常见的排序算法实现 算法描述 1.插入排序 从第一个元素开始,该元素可以认为已经被排序 取出下一个元素,在已经排序的元素序列中从后向前扫描 如果该元素(已排序)大于新元素,将该元素移到下一位置 重 ...

  10. Work Management Service application in SharePoint 2016

    最近开始弄SharePoint 2016的Workflow,遇到问题发现没有了Work Management Service application,然后用PowerShell命令创建也不行,bing ...