1 | #Region "Microsoft.VisualBasic::d0707d00d8702b95f89a654b32b25d8e, Microsoft.VisualBasic.Core\Net\HTTP\Base64Codec.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 | ' Module Base64Codec |
35 | ' |
36 | ' Function: __getImageFromBase64, __toBase64String, Base64String, DecodeBase64, GetImage |
37 | ' (+3 Overloads) ToBase64String, (+2 Overloads) ToStream |
38 | ' |
39 | ' |
40 | ' /********************************************************************************/ |
41 | |
42 | #End Region |
43 | |
44 | Imports System.Drawing |
45 | Imports System.Drawing.Imaging |
46 | Imports System.IO |
47 | Imports System.Runtime.CompilerServices |
48 | Imports System.Text |
49 | Imports Microsoft.VisualBasic.Imaging |
50 | Imports Microsoft.VisualBasic.Language |
51 | Imports Microsoft.VisualBasic.Text |
52 | |
53 | Namespace Net.Http |
54 | |
55 | ''' <summary> |
56 | ''' Tools API for encoded the image into a base64 string. |
57 | ''' </summary> |
58 | Public Module Base64Codec |
59 | |
60 | #Region "text" |
61 | |
62 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
63 | <Extension> |
64 | Public Function Base64String(text$, Optional encoding As Encoding = Nothing) As String |
65 | Return (encoding Or UTF8).GetBytes(text).ToBase64String |
66 | End Function |
67 | |
68 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
69 | <Extension> |
70 | Public Function DecodeBase64(base64$, Optional encoding As Encoding = Nothing) As String |
71 | Return (encoding Or UTF8).GetString(Convert.FromBase64String(base64)) |
72 | End Function |
73 | |
74 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
75 | <Extension> |
76 | Public Function ToBase64String(byts As IEnumerable(Of Byte)) As String |
77 | Return Convert.ToBase64String(byts.ToArray) |
78 | End Function |
79 | #End Region |
80 | |
81 | #Region "image/png" |
82 | |
83 | ''' <summary> |
84 | ''' Function to Get Image from Base64 Encoded String |
85 | ''' </summary> |
86 | ''' <param name="Base64String"></param> |
87 | ''' <param name="format"></param> |
88 | ''' <returns></returns> |
89 | <Extension> Public Function GetImage(Base64String As String, Optional format As ImageFormats = ImageFormats.Png) As Bitmap |
90 | Try |
91 | If String.IsNullOrEmpty(Base64String) Then |
92 | ' Checking The Base64 string validity |
93 | Return Nothing |
94 | Else |
95 | Return Base64String.__getImageFromBase64(GetFormat(format)) |
96 | End If |
97 | Catch ex As Exception |
98 | Call ex.PrintException |
99 | Return Nothing |
100 | End Try |
101 | End Function |
102 | |
103 | ''' <summary> |
104 | ''' Function to Get Image from Base64 Encoded String |
105 | ''' </summary> |
106 | ''' <param name="base64String"></param> |
107 | ''' <param name="format"></param> |
108 | ''' <returns></returns> |
109 | ''' |
110 | <Extension> |
111 | Private Function __getImageFromBase64(base64String$, format As ImageFormat) As Bitmap |
112 | Dim bytData As Byte(), streamImage As Bitmap |
113 | |
114 | ' Convert Base64 to Byte Array |
115 | bytData = Convert.FromBase64String(base64String) |
116 | |
117 | ' Using Memory stream to save image |
118 | Using ms As New MemoryStream(bytData) |
119 | ' Converting image from Memory stream |
120 | streamImage = Image.FromStream(ms) |
121 | End Using |
122 | |
123 | Return streamImage |
124 | End Function |
125 | |
126 | ''' <summary> |
127 | ''' Convert the Image from Input to Base64 Encoded String |
128 | ''' </summary> |
129 | ''' <param name="ImageInput"></param> |
130 | ''' <returns></returns> |
131 | <Extension> Public Function ToBase64String(ImageInput As Image, Optional format As ImageFormats = ImageFormats.Png) As String |
132 | Try |
133 | Return __toBase64String(ImageInput, GetFormat(format)) |
134 | Catch ex As Exception |
135 | Call ex.PrintException |
136 | Return "" |
137 | End Try |
138 | End Function |
139 | |
140 | ''' <summary> |
141 | ''' Convert the Image from Input to Base64 Encoded String |
142 | ''' </summary> |
143 | ''' <returns></returns> |
144 | ''' |
145 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
146 | <Extension> Public Function ToBase64String(bmp As Bitmap, Optional format As ImageFormats = ImageFormats.Png) As String |
147 | Return ToBase64String(ImageInput:=bmp, format:=format) |
148 | End Function |
149 | |
150 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
151 | <Extension> |
152 | Public Function ToStream(image As Image, Optional format As ImageFormats = ImageFormats.Png) As MemoryStream |
153 | Return image.ToStream(format.GetFormat) |
154 | End Function |
155 | |
156 | <Extension> |
157 | Public Function ToStream(image As Image, format As ImageFormat) As MemoryStream |
158 | With New MemoryStream |
159 | Call image.Save(.ByRef, format) |
160 | Call .Flush() |
161 | Call .Seek(Scan0, SeekOrigin.Begin) |
162 | Return .ByRef |
163 | End With |
164 | End Function |
165 | |
166 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
167 | Private Function __toBase64String(image As Image, format As ImageFormat) As String |
168 | Return Convert.ToBase64String(image.ToStream(format).ToArray) |
169 | End Function |
170 | #End Region |
171 | End Module |
172 | End Namespace |