在 vb.net 中将 base64 解码为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49507221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decode base64 to image in vb.net
提问by Winda Sari Elisabeth Siburian
I've been searching on the internet and have not found an answer. Would you like to tell me, how to decode from base64to be Imagelike line graph? I've been trying to convert from base64to Bytearray first and from Bytearray to Image.
我一直在互联网上搜索,但没有找到答案。你想告诉我,如何解码base64成Image折线图?我一直在尝试先从数组转换base64为Byte数组,再从Byte数组转换为Image.
Private Function convertbytetoimage(ByVal BA As Byte())
Dim ms As MemoryStream = New MemoryStream(BA)
image = Image.FromStream(ms) 'I always get wrong in this line.
Return image
End Function
回答by Paul Karam
Looking at your code, your problem could be using the variable name imageinstead of something like _image.
查看您的代码,您的问题可能是使用变量名称image而不是_image.
Keep in mind that VBis not case sensitivelike C#and other programming languages.
请记住,VB它不像其他编程语言那样区分大小写C#。
In your code, I assume you defined your imagevariable as Image.
在您的代码中,我假设您将image变量定义为Image.
To use the static Image.FromStream(ms), you either need to use the fully qualified name of Imageor change your variable name.
要使用 static Image.FromStream(ms),您需要使用完全限定名称Image或更改您的变量名称。
Here's how you can fix your code:
以下是修复代码的方法:
Private Function convertbytetoimage(ByVal BA As Byte())
Dim ms As MemoryStream = New MemoryStream(BA)
image = System.Drawing.Image.FromStream(ms)
Return image
End Function
Or you can do this by changing your variable name, such as:
或者您可以通过更改变量名称来实现,例如:
Dim _image as Image
Private Function convertbytetoimage(ByVal BA As Byte())
Dim ms As MemoryStream = New MemoryStream(BA)
_image = Image.FromStream(ms)
Return _image
End Function
Update:
更新:
You can try to convert the Bytearray to Imagealso by using ImageConvertor:
你可以尝试将转换Byte阵列Image使用也ImageConvertor:
Private Function convertbytetoimage(ByVal BA As Byte())
Dim converter As ImageConverter = New ImageConverter()
_image = CType(converter.ConvertFrom(BA), Image)
Return _image
End Function
Update 2:
更新 2:
Since it looks like that the main problem is with the base64string. Please have a look at my small demo that convert an Imagefrom inside a PictureBoxto base64string, then to Bytearray, and at the end, back to an Image.
因为看起来主要问题出在base64字符串上。请看一下我的小演示,它将 aImage从内部转换PictureBox为base64字符串,然后转换为Byte数组,最后再转换回Image.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim base64String = ConvertImageToBase64String() 'Using Functions To Make the code tidier
Dim byteArray = ConvertBase64ToByteArray(base64String) 'Using Functions To Make the code tidier
Dim image = convertbytetoimage(byteArray) 'Using Functions To Make the code tidier
PictureBox2.Image = image 'since we're using a small windows form app, we'll set back the image to a second picture box.
End Sub
Public Function ConvertImageToBase64String() As String
Using ms As New MemoryStream()
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png) 'We load the image from first PictureBox in the MemoryStream
Dim obyte = ms.ToArray() 'We tranform it to byte array..
Return Convert.ToBase64String(obyte) 'We then convert the byte array to base 64 string.
End Using
End Function
Public Function ConvertBase64ToByteArray(base64 As String) As Byte()
Return Convert.FromBase64String(base64) 'Convert the base64 back to byte array.
End Function
'Here's the part of your code (which works)
Private Function convertbytetoimage(ByVal BA As Byte())
Dim ms As MemoryStream = New MemoryStream(BA)
Dim image = System.Drawing.Image.FromStream(ms)
Return image
End Function
Note that after converting the Imageto base64string, it looks something like that (keep in mind that each image is different, hence you won't get the same string):
请注意,转换Image为base64字符串后,它看起来像这样(请记住,每个图像都不同,因此您不会得到相同的字符串):
/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQMBAAUAAAABAAAASgMDAAEAAAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAAAAAYagAACxj//bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAk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3HFfPngHwmPFeueTMSLWHDSY/jJPC5HIyAxz/s4yCQa+iItJtdIt7a0tYIVjV0QIiBV5YA8DA5yT9ea5cVQdWK5XZo3oVVTeuzDV9XRYHKsMAcmvn/AMdXV214wIJhm+ZpMdDk4X26Z96+i/EHhm2htWubpgiL8scaMcu3+FedalotjOhSVM57nkfjXJgcLOMnUqHTisRBwUIHg1FdR4n8LNpcbXttG32VZBFIAGIjYgleffaep7H8OXr1Tzz2D4OExWU829wBO42hztJ2pyR0JHY9sn1NesQT/atTsoyes6E/gc/0r5p8LeK5/DlwAQ8lqzF2SMqrbtuM5IOR0JHGdo5Fer6N4+0rUfLKXSw3G0nY3BB289fQE8+xoA7zxPq/9pagyo3+jw5VPf1NcXqMw55qefUoSnySKR6g1z9/fKc/OPzoA5vx0TJpgYOw5UkA/e7YP8/wrzuuz8VajDcWQgEqhwBwTyef/rVxlAH/2Q==
If you look at the first 5 characters of the string, you notice it's equal to /9j/4which means that the file represented by this string is a PNGfile, you may look at my old answerto see how to validatea base64string.
如果你看一下字符串的前5个字符,你注意到它的相等/9j/4,通过此字符串表示的文件是该装置PNG的文件,你可以看我以前的答案,看看如何验证一个base64字符串。
Another note, I used a small PNGimage, hence I used the next format:
另一个注意事项,我使用了一个小PNG图像,因此我使用了下一个格式:
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
If you have another Format and you want to use same code, make sure to change the format.
如果您有另一种格式并且想要使用相同的代码,请确保更改格式。

