FREE VB.NET SOURCE CODE

Showing posts with label base64. Show all posts
Showing posts with label base64. Show all posts

24 April, 2012

[SNP] Convert Base64 string to BItmap / Image / Picturebox.Image [VB.NET]

Okay, this is the second part of :
[SNP] Convert Image / PictureBox Image / Bitmap to string / text [VB.NET]


Here is a function, that will convert the Base64 string to Bitmap / Image back again.

But first:

Imports System.IO

Then:

 Public Function StringToBitmap(ByVal sImageData As String) As Bitmap
        Try
            Dim ms As New MemoryStream(Convert.FromBase64String(sImageData))
            Dim bmp As Bitmap = Bitmap.FromStream(ms)
            Return bmp
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

Use it like this:

 Dim IMG As Bitmap = StringToBitmap("PUT CONVERTED IMAGE'S STRING HERE")

Or like this:

   PictureBox1.Image = StringToBitmap("PUT CONVERTED IMAGE'S STRING HERE")

Do not forget, that if you put the image's string in a textbox, it may limit the default lenght of the textbox.text.


[SNP] Convert Image / PictureBox Image / Bitmap to string / text [VB.NET]

[SNP] Convert Image / PictureBox Image / Bitmap to string / text [VB.NET]

I am sorry for the long delay, but here we go again !

A code to convert your bitmap/picturebox image to string and another one to reverse it.

But you must: Imports System.IO first ;)

 Public Function BitmapToString(ByVal bImage As Bitmap) As String
        Try
            Dim data As String
            Dim ms As MemoryStream = New MemoryStream
            bImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            data = Convert.ToBase64String(ms.ToArray())

            Return data
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function

Use it as this:


        Dim STR As String = BitmapToString(BITMAPIMAGE)

Or like this:

  Dim STR As String = BitmapToString(PictureBox1.Image)

The next post will be about a function, to convert it back to image/bitmap

[SNP] Convert Base64 string to BItmap / Image / Picturebox.Image [VB.NET]