0

I'm new in VB.NET and I create an object image Dim myimg As Image that contains an image generated with VB.NET and showing in a picture box. I wanna add this image to HTML like this : Dim value$ = "<img src='myimg'> " Where value is used to replace tag in html file to show the picture generated in VB.NET

Is there a way to do this

Fares Omrani
  • 255
  • 1
  • 4
  • 22
  • 2
    Your source tag must have a location it won't be able to take in an object. You have to save the image first and with that location create your value$ variable. – washcloth Jun 24 '15 at 15:49
  • Alternatively, you might be able to [convert the image to base64](http://www.maryor.nl/blog/tabid/268/entryid/24/convert-an-image-to-a-base64-string-and-vice-versa.aspx) and [embed it in the browser](http://stackoverflow.com/questions/1207190/embedding-base64-images) – Rein S Jun 25 '15 at 19:27

1 Answers1

0

Borrowing from this question and this article's helper class, you can do something like this:

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

Within a button:

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 & """ />"
Community
  • 1
  • 1
Rein S
  • 889
  • 6
  • 18