Search code examples
vb.netvisual-studiodata-uridata-url

using DataURL in VB.NET


happy new year!

I just want to generate DataURL type data in VB.Net and also want to load string type DataURL to an image.

In other words, I want to convert an image in PictureBox(or Image) into string type DataURL vice versa.

The purpose of this kind of doubtful procedure is to save string data and image data into one single file.

Thank you!


Solution

  • Something like this:

    Use this function to get a Base64 string representation of the Image

    Public Function ToBase64String(ByVal aImage As Image) As String
        Using stream = New System.IO.MemoryStream
            aImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
            Return Convert.ToBase64String(stream.ToArray)
        End Using
    End Function
    

    Then use this to display it:

    <img src="data:image/png;base64, Base64StringHere" />