Solved

Convert Base64 string to jpeg

Posted on 2014-10-28
4
1,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial will teach you how to create simple effects and smooth transitions in your films. This will be demonstrated using Adobe Premiere Pro CS6.
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…

737 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