Share via


Converting Image stored as Hex to Jpeg

Question

Tuesday, September 17, 2013 5:01 PM

I exported some images that was stored as Hex from a database (using sql) to excel, how can i convert them to jpeg?

All replies (3)

Wednesday, September 18, 2013 5:09 AM âś…Answered | 2 votes

Hi Buddy,

From your description, I understood there is an issue regarding how to convert Image stored as Hex to Jpeg.

I created a sample to implement this function on my side. Something looks like this,

Imports System.IO

Module Module1

    Sub Main()

        'Hex data

        Dim str As String = "0XFFD8......"

        'remove 0X

        Dim data As String = str.Substring(2)

        Dim bytes As Byte() = New Byte(data.Length \ 2 - 1) {}

        For i As Integer = 0 To bytes.Length - 1

            bytes(i) = Convert.ToByte(data.Substring(i * 2, 2), 16)

        Next

        Dim fs As New FileStream("D:\picture.jpeg", FileMode.Create)

        fs.Write(bytes, 0, bytes.Length)

        fs.Close()

        Console.WriteLine("Successful")

        Console.ReadKey()

    End Sub

End Module

Here is a reference about how to read data from excel using vb.net. Hope it can help you to solve this issue better.

#How to Read Excel File AT VB.NET

http://forums.asp.net/t/1301233.aspx

On the other hand, I uploaded source code to SkyDrive. You can download from here.

If I misunderstood or my idea is incorrect, please feel free to let me know.

Have a nice day!

Sincerely,

<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support

Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


Tuesday, September 17, 2013 5:06 PM

"As Hex" means as a String. Is this correct?

What is your starting point? Do you already have the data in a variable, or do you need to read from Excel first? Or from the database?

Armin


Tuesday, September 17, 2013 5:31 PM

Convert your Hex string to bytes using Parse or TryParse specifying NumberStyles.HexNumber.  Construct a Bitmap from the bytes and save it as a JPEG.