Link to home
Start Free TrialLog in
Avatar of jpbivona
jpbivona

asked on

Decode hex text file into viewable bitmap image

I have an output text file with a hex sting, which is the result of capturing a signature image.

Does anyone have any insight as to how I can convert this file into a viewable Bitmap image file?

VB.net

Thanks.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you talking about a base-64 encoded string?

If so, then you can use something like this:

Imports System.IO
Imports System.Drawing.Imaging

Public Class ImageConverter

    Public Shared Function ByteArrayToImage(ByVal buffer() As Byte, ByVal fileName As String) As Image
        Using ms As New MemoryStream(buffer)
            Dim img As Image = Image.FromStream(ms)
            Return img
        End Using
    End Function

    Public Shared Function ImageToByteArray(ByVal img As Image, ByVal format As ImageFormat) As Byte()
        Using ms As New MemoryStream()
            img.Save(ms, format)
            Return ms.ToArray()
        End Using
    End Function

    Public Shared Function ImageToBase64String(ByVal img As Image, ByVal format As ImageFormat) As String
        Using ms As New MemoryStream()
            img.Save(ms, format)
            Return Convert.ToBase64String(ms.ToArray())
        End Using
    End Function

    Public Shared Function Base64StringToImage(ByVal imageString As String) As Image
        Dim buffer() As Byte = Convert.FromBase64String(imageString)
        Using ms As New MemoryStream(buffer)
            Return Image.FromStream(ms)
        End Using
    End Function

End Class

Bob

Avatar of jpbivona
jpbivona

ASKER

Getting Syntax error: Return Image.FromStream(ms)

'FromStream' is not a member of 'Imaging'

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America 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
Here is my VS About, I have -  Imports System.Drawing
Squiggly Blue Line
on
Return Image.FromStream(ms)
in
Base64StringToImage()

Microsoft Visual Studio 2005
Version 8.0.50727.762  (SP.050727-7600)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual Basic 2005   77626-009-1281673-41223
Microsoft Visual Basic 2005

Microsoft Visual C# 2005   77626-009-1281673-41223
Microsoft Visual C# 2005

Microsoft Visual C++ 2005   77626-009-1281673-41223
Microsoft Visual C++ 2005

Microsoft Visual J# 2005   77626-009-1281673-41223
Microsoft Visual J# 2005

Microsoft Visual Web Developer 2005   77626-009-1281673-41223
Microsoft Visual Web Developer 2005

Microsoft Web Application Projects 2005   77626-009-1281673-41223
Microsoft Web Application Projects 2005
Version 8.0.50727.762

Crystal Reports    AAC60-G0CSA4B-V7000AY
Crystal Reports for Visual Studio 2005


Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack 1 (KB926601)  
This service pack is for Microsoft Visual Studio 2005 Professional Edition - ENU.
If you later install a more recent service pack, this service pack will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/926601

SQL Server Analysis Services  
Microsoft SQL Server Analysis Services Designer
Version 9.00.1399.00

SQL Server Integration Services  
Microsoft SQL Server Integration Services Designer
Version 9.00.1399.00

SQL Server Reporting Services  
Microsoft SQL Server Reporting Services Designers
Version 9.00.1399.00
I find this very strange......I see example of using Image.FromStream()....but I have a syntax error even though I have 'Imports System.Drawing'

This seems kinda bad??
Ok, well try some fully-qualified references:

Public Class ImageConverter

  Public Shared Function ByteArrayToImage(ByVal data() As Byte, ByVal fileName As String) As Image
    Using ms As New System.IO.MemoryStream(Data)
      Dim img As Image = System.Drawing.Image.FromStream(ms)
      Return img
    End Using
  End Function

  Public Shared Function ImageToByteArray(ByVal img As System.Drawing.Image, ByVal format As System.Drawing.Imaging.ImageFormat) As Byte()
    Using ms As New System.IO.MemoryStream()
      img.Save(ms, format)
      Return ms.ToArray()
    End Using
  End Function

  Public Shared Function ImageToBase64String(ByVal img As System.Drawing.Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
    Using ms As New System.IO.MemoryStream()
      img.Save(ms, format)
      Return Convert.ToBase64String(ms.ToArray())
    End Using
  End Function

  Public Shared Function Base64StringToImage(ByVal imageString As String) As System.Drawing.Image
    Dim data() As Byte = Convert.FromBase64String(imageString)
    Using ms As New System.IO.MemoryStream(data)
      Return System.Drawing.Image.FromStream(ms)
    End Using
  End Function

End Class

Bob
Well, it seems the problem was that with a mobile device application, FromStream() in not a part of the .NET CF.

Oh well.
Hi,

does anybody know how to perform the same task in Delphi?
I also have an output text file with a hex sting, which is the result of capturing a signature image.
The string looks like below:

PRY7FTcUNBQyFjEYMhozHDYcNx84ITYjMyQxJS8kLSQtJP//QxJCFEIWQhlBG0EdQR9BIUMfQx///1YOVhBUD1IQUBBNEUwTSxZMGE0aTxxRHFMeVR9XHlgbWBlWGFMYURhPGE8Y//9gGl8YXxZfFF8SYBBgDmIPYxFkFGUXZhloFmgUaRFqDmoMagpqCv//Rz1HP0dBR

here is what the PDA vendor says about the file:
-----------------------------------------------------------------
Ok, the data coming in is basically a list of lines to draw.

To make the data in the first place, a series of points that would create the lines of the signature are recorded, where any pen lift (i.e. the drawing stopped) is recorded as 255,255 to serve as a terminator.

This array of points is turned into a byte array (i.e. line from 2,3 to 40, 50 to 80, 3 becomes byte[] {2,3,40,50, 80, 3} )

This is run through a Microsoft call Convert.ToBase64String to cut down on size (ever seen how big an xml encoded byte array is?) and make it easier to pass the data back into a webservice via a string parameter.  The data will need to be de-converted from this form to get the byte array back out, that is then used to draw the lines in the code sample you supplied.
-----------------------------------------------------------------

How to convert this file back into a viewable Bitmap image file?

thanks
Anthony