Link to home
Start Free TrialLog in
Avatar of JoeBo747
JoeBo747

asked on

Convert Base64 string to jpeg

How would I convert a base64 png encoded string to a jpeg image and then save the jpeg to sql 2008 image field?  The code below
results in a black square!

my code:

dim txt as string ="data:image/png;base64,iVBORw0KGgoAA etc......" ' <---this is a shortened version of a true base64 string

Dim ms As New MemoryStream(Convert.FromBase64String(txt))
           
Dim bmp As Bitmap = Bitmap.FromStream(ms) <---load bitmap from memory stream


            'create new memory stream
            Dim jpgms As New MemoryStream()
            jpgms.Position = 0
            bm.Save(jpgms, System.Drawing.Imaging.ImageFormat.Jpeg) <---convert to jpeg


jpgms.toArray() <--used as data source for sql 2008 image
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

You need to get rid of the header. Try something like this:
        Dim txt As String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" ' red dot from wikipedia
        If txt.StartsWith("data:") Then
            Dim poscomma As Integer = txt.IndexOf(",") + 1
            txt = txt.Substring(poscomma, txt.Length - poscomma)
        End If

Open in new window

Avatar of JoeBo747
JoeBo747

ASKER

Hi Robert,
Thanks for your reply,  the code supplied was copied accross from a trial app which does remove the header, sorry for the confusion.
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Robert,

Thanks for your assistance,