在 vb.net 中将 base64 解码为图像 [英] Decode base64 to image in vb.net
问题描述
我一直在互联网上搜索,但没有找到答案.你能告诉我,如何从 base64
解码成 Image
像折线图?我一直在尝试先从 base64
转换为 Byte
数组,然后从 Byte
数组转换为 Image
.>
私有函数convertbytetoimage(ByVal BA As Byte())Dim ms As MemoryStream = New MemoryStream(BA)image = Image.FromStream(ms) '我总是在这一行出错.返回图片结束函数
看看你的代码,你的问题可能是使用变量名 image
而不是 _image
.
请记住,VB
是不区分大小写 喜欢 C#
和其他编程语言.
在您的代码中,我假设您将 image
变量定义为 Image
.
要使用静态 Image.FromStream(ms)
,您需要使用 Image
的完全限定名称或更改您的变量名称.
以下是修复代码的方法:
私有函数convertbytetoimage(ByVal BA As Byte())Dim ms As MemoryStream = New MemoryStream(BA)图像 = System.Drawing.Image.FromStream(ms)返回图片结束函数
或者您可以通过更改变量名称来实现,例如:
Dim _image 作为图像私有函数 convertbytetoimage(ByVal BA As Byte())Dim ms As MemoryStream = New MemoryStream(BA)_image = Image.FromStream(ms)返回_image结束函数
更新:
您也可以尝试使用 ImageConvertor
:
私有函数convertbytetoimage(ByVal BA As Byte())Dim 转换器 As ImageConverter = New ImageConverter()_image = CType(converter.ConvertFrom(BA), Image)返回_image结束函数
更新 2:
因为看起来主要问题在于 base64
字符串.请看一下我的小演示,它将 Image
从 PictureBox
内部转换为 base64
字符串,然后到 Byte
> 数组,最后返回一个 Image
.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) 处理Button1.ClickDim base64String = ConvertImageToBase64String() '使用函数使代码更整洁Dim byteArray = ConvertBase64ToByteArray(base64String) '使用函数使代码更整洁Dim image = convertbytetoimage(byteArray) '使用函数使代码更整洁PictureBox2.Image = image '因为我们使用的是一个小的 windows 窗体应用程序,我们将把图像设置回第二个图片框.结束子公共函数 ConvertImageToBase64String() As String使用 ms 作为新的 MemoryStream()PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png) '我们从 MemoryStream 中的第一个 PictureBox 加载图像Dim obyte = ms.ToArray() '我们将其转换为字节数组..Return Convert.ToBase64String(obyte) '然后我们将字节数组转换为 base 64 字符串.结束使用结束函数公共函数 ConvertBase64ToByteArray(base64 As String) As Byte()Return Convert.FromBase64String(base64) '将 base64 转换回字节数组.结束函数'这是您的代码的一部分(有效)私有函数 convertbytetoimage(ByVal BA As Byte())Dim ms As MemoryStream = New MemoryStream(BA)昏暗的图像 = System.Drawing.Image.FromStream(ms)返回图片结束函数
请注意,将 Image
转换为 base64
字符串后,它看起来像这样(请记住,每个图像都不同,因此您不会得到相同的字符串):
如果您查看字符串的前 5 个字符,您会注意到它等于 /9j/4
,这意味着该字符串表示的文件是 PNG
文件,您可以查看我的旧答案以了解如何验证一个 base64
字符串.
另一个说明,我使用了一个小的 PNG
图像,因此我使用了下一个格式:
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
如果您有另一种格式并且想使用相同的代码,请务必更改格式.
I've been searching on the internet and have not found an answer. Would you like to tell me, how to decode from base64
to be Image
like line graph? I've been trying to convert from base64
to Byte
array first and from Byte
array to 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
Looking at your code, your problem could be using the variable name image
instead of something like _image
.
Keep in mind that VB
is not case sensitive like C#
and other programming languages.
In your code, I assume you defined your image
variable as Image
.
To use the static Image.FromStream(ms)
, you either need to use the fully qualified name of Image
or change your variable name.
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 Byte
array to Image
also by using 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:
Since it looks like that the main problem is with the base64
string. Please have a look at my small demo that convert an Image
from inside a PictureBox
to base64
string, then to Byte
array, and at the end, back to an 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 Image
to base64
string, it looks something like that (keep in mind that each image is different, hence you won't get the same string):
/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQMBAAUAAAABAAAASgMDAAEAAAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAAAAAYagAACxj//bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQfJzk9ODI8LjM0Mv/bAEMBCQkJDAsMGA0NGDIhHCEyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMv/AABEIADAAMAMBIgAC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htWubpgiL8scaMcu3+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/4
which means that the file represented by this string is a PNG
file, you may look at my old answer to see how to validate a base64
string.
Another note, I used a small PNG
image, hence I used the next format:
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.
这篇关于在 vb.net 中将 base64 解码为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!