Solved

Convert Base64 string to jpeg

Posted on 2014-10-28
4
938 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PaperPort (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) is among the most important applications that I run on my Windows computers. I use it every day, for nearly all of my document and photo scanning, as well as most of my…
This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
The goal of the tutorial is to teach the user what exposure is and how to use the exposure slider. Analyze the photo that you want to edit, then adjust the exposure slider to your liking.
Learn how to automatically add page numbers in your next InDesign project. This can be very helpful in multi-page books and magazines that you are designing. Make sure your Pages window visible.:  In the document you wish to add page numbers to. Act…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now