| 1 |
#Region "Microsoft.VisualBasic::d0707d00d8702b95f89a654b32b25d8e, Microsoft.VisualBasic.Core\Net\HTTP\Base64Codec.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
<Extension>
|
| 111 |
Private Function __getImageFromBase64(base64String$, format As ImageFormat) As Bitmap
|
| 112 |
Dim bytData As Byte(), streamImage As Bitmap
|
| 113 |
|
| 114 |
|
| 115 |
bytData = Convert.FromBase64String(base64String)
|
| 116 |
|
| 117 |
|
| 118 |
Using ms As New MemoryStream(bytData)
|
| 119 |
|
| 120 |
streamImage = Image.FromStream(ms)
|
| 121 |
End Using
|
| 122 |
|
| 123 |
Return streamImage
|
| 124 |
End Function
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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
|