Solved

Convert Base64 string to jpeg

Posted on 2014-10-28
4
1,007 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video player 10 111
Having a different time zone on your task bar 3 64
convert Photo To exact Size 10 94
Using MS Code on my Mac 6 68
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…
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…
The goal of this is to tech the user how to export photos out of Lightroom and the different options they have. Select which photos you want to export : Select where you want to export and with which options : Export :
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

770 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