Direct2D教程VIII——几何(Geometry)对象的运算,本系列的终结篇

目前博客园中成系列的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

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

image

 

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

image

代码和前面的代码几乎一样,仅仅是用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官网的说明图

image

image

image

 

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

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

posted @ 2013-09-03 20:44  万仓一黍  阅读(3500)  评论(1编辑  收藏  举报