Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4685
  • Last Modified:

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.
0
jpbivona
Asked:
jpbivona
  • 4
  • 3
1 Solution
 
Bob LearnedCommented:
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

0
 
jpbivonaAuthor Commented:
Getting Syntax error: Return Image.FromStream(ms)

'FromStream' is not a member of 'Imaging'

0
 
Bob LearnedCommented:
On what line?   What version of .NET do you have?  That should have been coming from System.Drawing,  not System.Drawing.Imaging.

Bob
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
jpbivonaAuthor Commented:
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
0
 
jpbivonaAuthor Commented:
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??
0
 
Bob LearnedCommented:
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
0
 
jpbivonaAuthor Commented:
Well, it seems the problem was that with a mobile device application, FromStream() in not a part of the .NET CF.

Oh well.
0
 
COS_IT_AUCommented:
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
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now