VB.net 从字符串到位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28237380/
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
VB.net from string to bitmap
提问by Walt501
I'm using vb.net framework 4.5 . From a 3th party I receive a CSV file. In one block their is a BMP image. I read it from the file as a string.
我正在使用 vb.net 框架 4.5 。我从第 3 方收到一个 CSV 文件。在一个块中,它们是 BMP 图像。我从文件中读取它作为字符串。
I want to convert this BMP to a JPG. I learned about the bitmap class in the .net framework. This could represent a bitmap and save it in the JPG format. However the bitmap class constructor takes a filename a parameter. That's not what I want. I tried to convert the string to an IO memory stream and read it into the bitmap class but this isn't working.
我想将此 BMP 转换为 JPG。我了解了 .net 框架中的位图类。这可以表示位图并将其保存为 JPG 格式。然而,位图类构造函数将文件名作为参数。那不是我想要的。我试图将字符串转换为 IO 内存流并将其读入位图类,但这不起作用。
Can someone give me some pointer on methodes / classes to use to achieve this ?
有人可以给我一些关于用于实现此目的的方法/类的指针吗?
回答by Idle_Mind
Hard to give code when we don't know the format of the string...
当我们不知道字符串的格式时,很难给出代码......
...it might be a base 64 encoded string. Here's an example that shows the image in PictureBox1 being converted to a base 64 string, then back to an image and placed into PictureBox2:
...它可能是一个 base 64 编码的字符串。这是一个示例,显示 PictureBox1 中的图像被转换为 base 64 字符串,然后返回图像并放入 PictureBox2:


Dim oldMS As New System.IO.MemoryStream
PictureBox1.Image.Save(oldMS, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim strData As String = Convert.ToBase64String(oldMS.ToArray)
Debug.Print(strData)
Dim bytes() As Byte = Convert.FromBase64String(strData) ' strData would come from your CSV file
Dim MS As New System.IO.MemoryStream(bytes)
Dim bmp As Image = Image.FromStream(MS)
PictureBox2.Image = bmp
Here's what the string looks like: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P8Az8S/99mur8Pa9rngzW9OvIL6RklZfPt95KkHBKMDxnawPtn2rE0vTd08E91EzwlspAv358dh6D1J/Wr+sXcv261s5PKD+cLiVYjkBmxgbu5xzn/ax2FTJJqw09TFa4ktdVe4iOJI5iynHvXeeGYrDUDDNfTTJJGjTxWSRbmkReSqyZ4DYIww/wDr874v8I6t4a8QXVpdWkxjMjGGYISsik5BBHtWZaz6haxSQrFP5Mi7XUKQcexxx0FHM+X3WEopvU6XxAtlZ3V39keZyQJp7V02sFbBAaTPIGQMKBXJRzSXOqJNKcu8wZjj3qe7lv7pEiMM4hjAVEKk8D1Pc8n+mK0/CPhLVvEmv2tpaWk2zzFMszIQsag5JJ+naknaPvMdknof/9k=
回答by Walt501
Thanks for all the inspiration. This is what I did and what worked.
感谢所有的灵感。这就是我所做的和有效的。
Dim bytes() As Byte
Dim MS As System.IO.MemoryStream
Dim ImageInStringFormat as string
Dim NewImage As System.Drawing.Image
bytes = Convert.FromBase64String(ImageInStringFormat)
MS = New System.IO.MemoryStream(bytes)
NewImage = System.Drawing.Image.FromStream(MS)
NewImage.Save("c:\temp\try.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Last remark, It turned out that the string I was using wasn't a valid Base64 string. You can check this by getting the length of the string and check if it can be divided by 4. Of not, at = to the end of the string until it is dividable by 4. This will bake it a valid Base64 string.
最后一句话,原来我使用的字符串不是有效的 Base64 字符串。您可以通过获取字符串的长度并检查它是否可以被 4 整除来检查这一点。当然,在= 到字符串的末尾,直到它可以被 4 整除。这将使其成为有效的 Base64 字符串。
回答by Cr4xy
A solution without having to save it to the filesystem:
I combined this question's answers + How to Convert System.Drawing.Image to Byte Array?to this:
无需将其保存到文件系统的解决方案:
我结合了这个问题的答案 +如何将 System.Drawing.Image 转换为字节数组?对此:
Function ImageToString(ByVal Img As Image)
Dim ImgConverter As New ImageConverter()
Dim ImgBytes As Byte() = ImgConverter.ConvertTo(Img, GetType(Byte()))
Return Convert.ToBase64String(ImgBytes)
End Function
I hope it helps someone :)
我希望它可以帮助某人:)
回答by CodeCaster
Bitmap also has a constructor accepting a Stream. You can read the data into a MemoryStreamand pass that to the constructor.
Bitmap 也有一个接受Stream. 您可以将数据读入 aMemoryStream并将其传递给构造函数。
How to implement this depends on your code, which you didn't provide.
如何实现这取决于您没有提供的代码。
回答by Russ Wilson
Please refer to thispost. Assuming your string is a valid string of bytes representing the image, this post should give you what you're looking for. The code examples are in C#, but they are simple enough to convert to VB.Net.
请参考这个帖子。假设您的字符串是表示图像的有效字节字符串,这篇文章应该会为您提供所需的内容。代码示例使用 C#,但它们非常简单,可以转换为 VB.Net。

