我是VB.NET新手,我创建了一个对象图像Dim myimg As Image,其中包含一个用VB.NET生成并显示在图片框中的图像。我想将这个图片添加到超文本标记语言中,如下所示:Dim value$ = "<img src='myimg'>“,其中的值用于替换HTML文件中的标记,以显示在VB.NET中生成的图片
有没有办法做到这一点?
发布于 2015-06-26 04:00:20
借用this question和this article's helper类,您可以执行以下操作:
Imports System.IO
Imports System.Drawing.Imaging
Public Class ImageHelper
' Convert an image to a Base64 string by using a MemoryStream. Save the
' image to the MemoryStream and use Convert.ToBase64String to convert
' the content of that MemoryStream to a Base64 string.
Public Shared Function ImageToBase64String(ByVal image As Image, _
ByVal imageFormat As ImageFormat)
Using memStream As New MemoryStream
image.Save(memStream, imageFormat)
Dim result As String = Convert.ToBase64String(memStream.ToArray())
memStream.Close()
Return result
: End Using
End Function
' Convert a Base64 string back to an image. Fill a MemorySTream based
' on the Base64 string and call the Image.FromStream() methode to
' convert the content of the MemoryStream to an image.
Public Shared Function ImageFromBase64String(ByVal base64 As String)
Using memStream As New MemoryStream(Convert.FromBase64String(base64))
Dim result As Image = Image.FromStream(memStream)
memStream.Close()
Return result
End Using
End Function
End Class在一个按钮中:
Dim myImage As Image = Image.FromFile("C:\test.png")
Dim base64 As String = ImageHelper.ImageToBase64String(myImage, ImageFormat.Png)
WebBrowser1.DocumentText = "<img alt=""Embedded Image"" src=""data:image/png;base64," & base64 & """ />"https://stackoverflow.com/questions/31031021
复制相似问题