1 #Region "Microsoft.VisualBasic::2859828fb18f77637487d9fdec0ee388, Microsoft.VisualBasic.Core\Extensions\Image\GDI+\Graphics2D.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class Graphics2D
35     
36     '         Properties: Center, Height, ImageResource, Size, Width
37     
38     '         Constructor: (+3 OverloadsSub New
39     
40     '         Function: CreateDevice, CreateObject, Open, (+2 Overloads) Save, ToString
41     
42     '         Sub: __save, DrawCircle
43     '         Structure Context
44     
45     '             Function: Create
46     
47     
48     
49     
50     ' /********************************************************************************/
51
52 #End Region
53
54 Imports System.Drawing
55 Imports System.Drawing.Imaging
56 Imports System.Reflection
57 Imports Microsoft.VisualBasic.Language
58
59 Namespace Imaging
60
61     ''' <summary>
62     ''' GDI+ device handle for encapsulates a GDI+ drawing surface.
63     ''' (GDI+绘图设备句柄,这个对象其实是为了将gdi+绘图与图形模块的SVG绘图操作统一起来的)
64     ''' </summary>
65     ''' <remarks></remarks>
66     Public Class Graphics2D : Inherits GDICanvas
67         Implements IDisposable
68
69         ''' <summary>
70         ''' GDI+ device handle memory.(GDI+设备之中的图像数据)
71         ''' </summary>
72         ''' <remarks></remarks>
73         Public Property ImageResource As Image
74             Get
75                 Return innerImage
76             End Get
77             Protected Friend Set(value As Image)
78                 innerImage = value
79
80                 If Not value Is Nothing Then
81                     _Size = value.Size
82                     _Center = New Point(Size.Width / 2, Size.Height / 2)
83                 Else
84                     _Size = Nothing
85                     _Center = Nothing
86                 End If
87             End Set
88         End Property
89
90         Dim innerImage As Image
91
92         Protected Sub New()
93         End Sub
94
95         Sub New(size As Size, fill As Color)
96             Dim base = size.CreateGDIDevice(fill)
97
98             Stroke = base.Stroke
99             Font = base.Font
100             ImageResource = base.ImageResource
101             Graphics = base.Graphics
102         End Sub
103
104         Sub New(context As Context)
105             Call Me.New(context.size, context.color.TranslateColor)
106         End Sub
107
108         ''' <summary>
109         ''' Can be serialize as a XML file node.
110         ''' </summary>
111         Public Structure Context
112             Dim size As Size
113             Dim color$
114
115             Public Function Create() As Graphics2D
116                 Return size.CreateGDIDevice(color.TranslateColor)
117             End Function
118         End Structure
119
120         Public ReadOnly Property Width As Integer
121             Get
122                 Return Size.Width
123             End Get
124         End Property
125
126         Public ReadOnly Property Height As Integer
127             Get
128                 Return Size.Height
129             End Get
130         End Property
131
132         ''' <summary>
133         ''' Gets the width and height, in pixels, of this <see cref="ImageResource"/>.(图像的大小)
134         ''' </summary>
135         ''' <returns>A <see cref="System.Drawing.Size"/> structure that represents the width and height, in pixels,
136         ''' of this image.</returns>
137         Public Overrides ReadOnly Property Size As Size
138
139         ''' <summary>
140         ''' 在图象上面的中心的位置点
141         ''' </summary>
142         ''' <returns></returns>
143         Public ReadOnly Property Center As Point
144
145         ''' <summary>
146         ''' 将GDI+设备之中的图像数据保存到指定的文件路径之中,默认的图像文件的格式为PNG格式
147         ''' </summary>
148         ''' <param name="Path"></param>
149         ''' <param name="Format">默认为png格式</param>
150         ''' <returns></returns>
151         Public Overloads Function Save(path$, Optional Format As ImageFormats = ImageFormats.Png) As Boolean
152             Return Save(path, Format.GetFormat)
153         End Function
154
155         ''' <summary>
156         ''' 将GDI+设备之中的图像数据保存到指定的文件路径之中,默认的图像文件的格式为PNG格式
157         ''' </summary>
158         ''' <param name="Path"></param>
159         ''' <param name="Format">默认为png格式</param>
160         ''' <returns></returns>
161         Public Overloads Function Save(path$, Optional Format As ImageFormat = NothingAs Boolean
162             Try
163                 Call __save(path, Format Or Png)
164             Catch ex As Exception
165                 Return App.LogException(ex, MethodBase.GetCurrentMethod.GetFullName)
166             End Try
167
168             Return True
169         End Function
170
171         Private Sub __save(path As String, format As ImageFormat)
172             Call path.ParentPath.MkDIR
173             Call ImageResource.Save(path, format)
174         End Sub
175
176         Public Overrides Function ToString() As String
177             Return Size.ToString
178         End Function
179
180         ''' <summary>
181         ''' 
182         ''' </summary>
183         ''' <param name="r"></param>
184         ''' <param name="filled">所填充的颜色</param>
185         ''' <returns></returns>
186         Public Shared Function CreateDevice(r As Size, Optional filled As Color = NothingAs Graphics2D
187             Return r.CreateGDIDevice(filled)
188         End Function
189
190         ''' <summary>
191         ''' Get the internal <see cref="ImageResource"/>
192         ''' </summary>
193         ''' <param name="g2D"></param>
194         ''' <returns></returns>
195         Public Shared Narrowing Operator CType(g2D As Graphics2D) As Image
196             Return g2D.ImageResource
197         End Operator
198
199         Public Shared Widening Operator CType(img As Image) As Graphics2D
200             Dim g As Graphics = Graphics.FromImage(img)
201             Return CreateObject(g, res:=img)
202         End Operator
203
204         Public Shared Widening Operator CType(img As Bitmap) As Graphics2D
205             Dim g As Graphics = Graphics.FromImage(img)
206             Return CreateObject(g, img)
207         End Operator
208
209         ''' <summary>
210         ''' Internal create gdi device helper.(这个函数不会克隆原来的图像对象<paramref name="res"/>)
211         ''' </summary>
212         ''' <param name="g"></param>
213         ''' <param name="res">绘图的基础图像对象</param>
214         ''' <returns></returns>
215         Friend Shared Function CreateObject(g As Graphics, res As Image) As Graphics2D
216             With New Graphics2D With {
217                 .ImageResource = res,
218                 .g = g,
219                 .Font = New Font(FontFace.MicrosoftYaHei, 12),
220                 .Stroke = Pens.Black,
221                 .InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic,
222                 .PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality,
223                 .CompositingQuality = Drawing2D.CompositingQuality.HighQuality,
224                 .SmoothingMode = Drawing2D.SmoothingMode.HighQuality,
225                 .TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
226             }
227                 ' .Clear(Color.Transparent)
228                 Return .ByRef
229             End With
230         End Function
231
232         ''' <summary>
233         ''' Creates a new <see cref="System.Drawing.Graphics"/> from the specified <see cref="Image"/>.
234         ''' </summary>
235         ''' <param name="image">
236         ''' <see cref="Image"/> from which to create the new System.Drawing.Graphics.
237         ''' </param>
238         ''' <returns>
239         ''' This method returns a new <see cref="System.Drawing.Graphics"/> for the specified <see cref="Image"/>.
240         ''' </returns>
241         Public Shared Function Open(image As Image) As Graphics2D
242             Dim g As Graphics = Graphics.FromImage(image)
243             Return Graphics2D.CreateObject(g, image)
244         End Function
245
246         Public Overrides Sub DrawCircle(center As PointF, fill As Color, stroke As Pen, radius As Single)
247             Throw New NotImplementedException()
248         End Sub
249     End Class
250 End Namespace