Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert bytes to image.

Status
Not open for further replies.

vanion2903

IS-IT--Management
Jun 16, 2009
5
0
0
US
I've been working on a this problem for about a week now and have gotten nowhere. We have a test machine in our shop that stores data in an Access database. The main data I want is stored as an OLE Object. The company that makes the software says the data is stored as bytes. I need to extract those bytes out of the database to display as images in an excel spreadsheet. I have looked all over the web and can't find anything that works. I have been about to save the files to disk, but the image doesn't display. The software company is able to read the data and save the images to a Word document, but I'm not sure how they are doing it. I have very little experience with VB.net. Can someone please help me with this? I can send the code I've been trying if need be. Thanks!
 
Here's an example. For illustration purposes, I'm not connecting to a database. I'm creating a DataTable in memory, and saving an image in the DataTable as a byte array. Then, I'm reading the byte array back out of the table, and saving it as a new image on the C:\ drive. In your scenario, the DataTable would be filled from your database.
Code:
        'Create a DataTable with an "Image" column
        Dim DT As New DataTable
        DT.Columns.Add("Image", System.Type.GetType("System.Byte[]"))
        'Add a record to that DataTable by select a jpeg
        Dim OFD As New OpenFileDialog
        OFD.Filter = "JPEG Images|*.jpg"
        OFD.ShowDialog()
        Dim BYTES As Byte()
        If OFD.FileName.Trim <> "" Then
            BYTES = System.IO.File.ReadAllBytes(OFD.FileName)
            DT.Rows.Add(New Object() {BYTES})
        End If
        'Scroll through the records and save out the images to the C:\ drive
        Dim i As Integer = 0
        For Each dr As DataRow In DT.Rows
            Dim ImageData As Byte() = dr.Item("Image")
            System.IO.File.WriteAllBytes("C:\Record" & i.ToString.PadLeft(3, "0") & ".jpg", ImageData)
            i += 1
        Next
 
Thanks for the reply RiverGuy. Unfortunately that doesn't work for me. I get a file written to the hard drive but when I open it, I get the No Preview Available message. The software company says the data is written to Access as 512 bytes, if that helps any. I really don't understand why this is being so hard for me. I'm just wondering if they are actually writing to the database a different way.
 
Try saving the file with the extension which represents the image type your vendor is saving. For example, if it's a png, use .png instead of .jpg.
 
You might take a look here:
They market a number of image and pdf tools. You might download and try Informatik Image Viewer (the standard version is FREE). Also take a look at Informatik Reformat, which can convert files between formats. I've used it in the past with good results. ZIt's not free, but there is atrial version you can look at.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top