目前博客园中成系列的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. STM32F4 SPI with DMA

    STM32F4 SPI with DMA A few people have requested code, so I thought I’d post the code showing how I’ ...

  2. python服务端内存泄露的处理过程

    http://xiaorui.cc http://xiaorui.cc/2017/08/20/python服务端内存泄露的处理过程/

  3. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. NSNotification的使用(对观察者模式最通俗、易懂的讲解)

    这是一个观察者模式. 首先在你需要监听的类中加入观察者: - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString ...

  5. LeetCode——Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  6. python测试开发django-42.auth模块登陆认证

    前言 在开发一个网站时,经常会用到用户的注册和登陆相关的账号管理功能,auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. 像用户注册.用户登录.用户认证.注销 ...

  7. gzip格式解压缩

    gzip格式解压缩 有时候网络请求中会出现gzip格式的数据,而我们无法通过常规办法进行解析: 这时候可以使用下面的这个工具来解决这个问题: https://github.com/mattt/Godz ...

  8. 《Erlang程序设计(第2版)》

    <Erlang程序设计(第2版)> 基本信息 作者: (瑞典)Joe Armstrong 译者: 牛化成 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9787115354 ...

  9. Tornado开发技巧,简单了解tornado

    tornado基础入门(一)——简单了解tornado 参考:http://demo.pythoner.com/itt2zh/ch1.html tornado是一个轻量级的web框架,是一个用pyth ...

  10. 轻松看懂Java字节码

    java字节码 计算机只认识0和1.这意味着任何语言编写的程序最终都需要经过编译器编译成机器码才能被计算机执行.所以,我们所编写的程序在不同的平台上运行前都要经过重新编译才能被执行. 而Java刚诞生 ...