gpt4 book ai didi

arrays - 在vb.net中将base64解码为图像

转载 作者:行者123 更新时间:2023-12-02 03:38:09 26 4
gpt4 key购买 nike

我一直在互联网上搜索,但没有找到答案。您想告诉我,如何从 base64 解码为像线图一样的 Image 吗?我一直在尝试首先从 base64 转换为 Byte 数组,然后从 Byte 数组转换为 Image。 p>

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

最佳答案

查看您的代码,您的问题可能是使用变量名称 image 而不是 _image 之类的名称。

请记住,VB 是 not case sensitive就像 C# 和其他编程语言一样。

在您的代码中,我假设您将 image 变量定义为 Image

要使用静态 Image.FromStream(ms),您需要使用 Image 的完全限定名称或更改变量名称。

以下是修复代码的方法:

Private Function convertbytetoimage(ByVal BA As Byte())
Dim ms As MemoryStream = New MemoryStream(BA)
image = System.Drawing.Image.FromStream(ms)
Return image
End Function

或者您可以通过更改变量名称来完成此操作,例如:

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

#更新:

您也可以尝试使用 ImageConvertorByte 数组转换为 Image :

Private Function convertbytetoimage(ByVal BA As Byte())
Dim converter As ImageConverter = New ImageConverter()
_image = CType(converter.ConvertFrom(BA), Image)
Return _image
End Function

#更新2:

因为看起来主要问题在于 base64 字符串。请看一下我的小演示,它将 ImagePictureBox 内部转换为 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

请注意,将 Image 转换为 base64 字符串后,它看起来像这样(请记住,每个图像都是不同的,因此您不会得到相同的图像)字符串):

/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQMBAAUAAAABAAAASgMDAAEAAAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAAAAAYagAACxj//bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQfJzk9ODI8LjM0Mv/bAEMBCQkJDAsMGA0NGDIhHCEyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMv/AABEIADAAMAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAM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它等于 /9j/4,这意味着该字符串表示的文件是 PNG文件,您可以查看my old answer了解如何验证 base64 字符串。

另一个注意事项,我使用了一个小的 PNG 图像,因此我使用了下一个格式:

PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png) 

如果您有其他格式并且想要使用相同的代码,请务必更改格式。

关于arrays - 在vb.net中将base64解码为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com