簡體   English   中英

將VB.NET Image對象添加到HTML

[英]Add VB.NET Image object to HTML

我是VB.NET的新手,我創建了一個對象圖像Dim myimg As Image ,它包含用VB.NET生成並顯示在圖片框中的圖像。 我想像這樣將圖像添加到HTML: Dim value$ = "<img src='myimg'> ”其中,值用於替換html文件中的標記以顯示在VB.NET中生成的圖片。

有沒有辦法做到這一點

這個問題本文的幫助器類中借用,您可以執行以下操作:

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 & """ />"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM