Solved

Convert Base64 string to jpeg

Posted on 2014-10-28
4
1,084 Views
Last Modified: 2014-10-28
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
0
Comment
Question by:JoeBo747
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40408943
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

0
 
LVL 1

Author Comment

by:JoeBo747
ID: 40409119
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.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40409727
Ok, so could you post the entire base64 string or is the image sensitive?

I tested with the following code which simply puts the image in a picturebox on a form, if you could test that, then at least we could probably assume the problem is in the database code in which case I would ask you to post a bit more of your code regarding that part of the program.
Imports System.IO

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            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

            Dim ms As New MemoryStream(Convert.FromBase64String(txt))

            Dim bm 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

            PictureBox1.Image = Image.FromStream(jpgms)

            'jpgms.toArray() '<--used as data source for sql 2008 image
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:JoeBo747
ID: 40409979
Hi Robert,

Thanks for your assistance,
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The goal of the tutorial is to teach the user how to import photos into Adobe Lightroom efficiently and to keep everything organized.
The goal of the tutorial is to teach the user how to use import presets downloaded from the internet into Adobe Lightroom. Once you downloaded the presets go into the preset folder and press import then import your preset and your set it to go.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question