1 #Region "Microsoft.VisualBasic::d2fbac009c5a6f0dc8a43ac3bdaa8e66, Microsoft.VisualBasic.Core\Extensions\Image\ImageFormat.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     '     Enum ImageFormats
35     
36     '         Base64, Bmp, Emf, Exif, Gif
37     '         Icon, Jpeg, MemoryBmp, Png, Tiff
38     '         Wmf
39     
40     '  
41     
42     
43     
44     '     Module ImageFormatExtensions
45     
46     '         Properties: Png
47     
48     '         FunctionGetFormat, GetSaveImageFormat, ParseImageFormat, SaveAs
49     
50     
51     ' /********************************************************************************/
52
53 #End Region
54
55 Imports System.Drawing
56 Imports System.Drawing.Imaging
57 Imports System.Runtime.CompilerServices
58 Imports Microsoft.VisualBasic.Net.Http
59 Imports Microsoft.VisualBasic.Text
60 Imports defaultFormat = Microsoft.VisualBasic.Language.Default.DefaultValue(Of System.Drawing.Imaging.ImageFormat)
61
62 Namespace Imaging
63
64     ''' <summary>
65     ''' Specifies the file format of the image.
66     ''' </summary>
67     Public Enum ImageFormats As Integer
68
69         ''' <summary>
70         ''' Gets the bitmap (BMP) image format.
71         ''' </summary>
72         Bmp
73         ''' <summary>
74         ''' Gets the enhanced metafile (EMF) image format.
75         ''' </summary>
76         Emf
77         ''' <summary>
78         ''' Gets the Exchangeable Image File (Exif) format.
79         ''' </summary>
80         Exif
81         ''' <summary>
82         ''' Gets the Graphics Interchange Format (GIF) image format.
83         ''' </summary>
84         Gif
85         ''' <summary>
86         ''' Gets the Windows icon image format.
87         ''' </summary>
88         Icon
89         ''' <summary>
90         ''' Gets the Joint Photographic Experts Group (JPEG) image format.
91         ''' </summary>
92         Jpeg
93         ''' <summary>
94         ''' Gets the format of a bitmap in memory.
95         ''' </summary>
96         MemoryBmp
97         ''' <summary>
98         ''' Gets the W3C Portable Network Graphics (PNG) image format.
99         ''' </summary>
100         Png
101         ''' <summary>
102         ''' Gets the Tagged Image File Format (TIFF) image format.
103         ''' </summary>
104         Tiff
105         ''' <summary>
106         ''' Gets the Windows metafile (WMF) image format.
107         ''' </summary>
108         Wmf
109         ''' <summary>
110         ''' Base64
111         ''' </summary>
112         Base64
113     End Enum
114
115     ''' <summary>
116     ''' Specifies the file format of the image. Not inheritable.
117     ''' </summary>
118     Public Module ImageFormatExtensions
119
120         ''' <summary>
121         ''' 获取 W3C 可移植网络图形 (PNG) 图像格式。
122         ''' </summary>
123         ''' <returns></returns>
124         Public ReadOnly Property Png As defaultFormat = ImageFormat.Png
125
126         ''' <summary>
127         ''' default is <see cref="ImageFormat.Png"/>
128         ''' </summary>
129         ''' <param name="format">大小写不敏感</param>
130         ''' <returns></returns>
131         <Extension>
132         Public Function GetSaveImageFormat(format As StringAs ImageFormat
133             Dim value As String = format.ToLower.Trim
134
135             If ImagingFormats.ContainsKey(value) Then
136                 Return ImagingFormats(value)
137             Else
138                 Return ImageFormat.Png
139             End If
140         End Function
141
142         ReadOnly ImagingFormats As New Dictionary(Of String, ImageFormat) From {
143             {"jpg", ImageFormat.Jpeg},
144             {"bmp", ImageFormat.Bmp},
145             {"emf", ImageFormat.Emf},
146             {"exif", ImageFormat.Exif},
147             {"gif", ImageFormat.Gif},
148             {"png", ImageFormat.Png},
149             {"wmf", ImageFormat.Wmf},
150             {"tiff", ImageFormat.Tiff}
151         }
152
153         <MethodImpl(MethodImplOptions.AggressiveInlining)>
154         <Extension> Public Function GetFormat(format As ImageFormats) As ImageFormat
155             Return __formats(format)
156         End Function
157
158         Dim enumFormats As Dictionary(Of String, ImageFormats) =
159             [Enums](Of ImageFormats)() _
160             .ToDictionary(Function(t) t.ToString.ToLower)
161
162         ''' <summary>
163         ''' 不存在的名称会返回<see cref="ImageFormats.Png"/>类型
164         ''' </summary>
165         ''' <param name="format$">大小写不敏感</param>
166         ''' <returns></returns>
167         ''' 
168         <MethodImpl(MethodImplOptions.AggressiveInlining)>
169         <Extension>
170         Public Function ParseImageFormat(format$) As ImageFormats
171             Return enumFormats.TryGetValue(LCase(format), [default]:=ImageFormats.Png)
172         End Function
173
174         ReadOnly __formats As New SortedDictionary(Of ImageFormats, ImageFormat) From {
175  _
176             {ImageFormats.Bmp, ImageFormat.Bmp},
177             {ImageFormats.Emf, ImageFormat.Emf},
178             {ImageFormats.Exif, ImageFormat.Exif},
179             {ImageFormats.Gif, ImageFormat.Gif},
180             {ImageFormats.Icon, ImageFormat.Icon},
181             {ImageFormats.Jpeg, ImageFormat.Jpeg},
182             {ImageFormats.MemoryBmp, ImageFormat.MemoryBmp},
183             {ImageFormats.Png, ImageFormat.Png},
184             {ImageFormats.Tiff, ImageFormat.Tiff},
185             {ImageFormats.Wmf, ImageFormat.Wmf}
186         }
187
188         ''' <summary>
189         ''' Saves this <see cref="System.Drawing.Image"/> to the specified file in the specified format.
190         ''' (这个函数可以很容易的将图像对象保存为tiff文件)
191         ''' </summary>
192         ''' <param name="res">
193         ''' The image resource data that will be saved to the disk.
194         ''' (因为这个函数可能会被<see cref="Graphics2D.ImageResource"/>所调用,
195         ''' 由于该属性的Set方法是不公开可见的,所以将会不兼容这个方法,如果这个
196         ''' 参数被设置为ByRef的话)
197         ''' </param>
198         ''' <param name="path">path string</param>
199         ''' <param name="format">Image formats enumeration.</param>
200         ''' <returns></returns>
201         <Extension> Public Function SaveAs(res As Image,
202                                            path$,
203                                            Optional format As ImageFormats = ImageFormats.Png,
204                                            Optional autoDispose As Boolean = FalseAs Boolean
205             Try
206                 Call path.ParentPath.MkDIR
207
208                 If format = ImageFormats.Tiff Then
209                     Return New TiffWriter(res).MultipageTiffSave(path)
210                 ElseIf format = ImageFormats.Base64 Then
211                     Return res _
212                         .ToBase64String _
213                         .SaveTo(path, Encodings.ASCII.CodePage)
214                 Else
215                     Call res.Save(path, format.GetFormat)
216                 End If
217             Catch ex As Exception
218                 ex = New Exception(path.ToFileURL, ex)
219                 Call App.LogException(ex)
220                 Call ex.PrintException
221                 Return False
222             Finally
223                 If autoDispose Then
224                     Call res.Dispose()
225                     Call GC.SuppressFinalize(res)
226                     Call GC.Collect()
227                 End If
228             End Try
229
230             Return True
231         End Function
232     End Module
233 End Namespace