Converting blob to jpeg using VB6

I know that VB6 is already 'long' ago. And people now converting into .NET. But since I only know VB6 right now and never do any program in .NET, so, when people want a software in such a short time, you need to use the platform that you really know well.

I've volunteered myself to build a program to convert image data that is store in the database (using blob or other binary type) back into image file (in a sense jpeg, bitmap, gif, tiff or png). So, this is the piece of code on how to do that. You guys should appreciate it you know because I spend 2 whole days to find this solution (yeah, yeah, I know..sometimes I'm so late in digesting information)

This program will compare the information in the database. If the info exist, the program will take only the pics. I change the variable name, table name and everything because I don't want you to know what I'm doing.


' of course you need to establish connection into the database first. I'm not
' going to tell you about it, since most people can do it

sqlCommand = "SELECT picture from tblImage WHERE pictureID='handsomeGuy'"

rs.Open sqlCommand

if rs.EOF then
' tell user to do something
else
' in this case we assume only one binary data exist in the database
Dim tempBinary() As Byte
Dim filename As String

filename = "somepath\" & "somefile.jpg"

' this is using ADO, if you use DAO, look the statement below
tempBinary() = rs.Fields(0).GetChunk(rs.Fields(0).ActualSize)

' this statement using DAO
' tempBinary() = rs!FieldsName.GetChunk(0, rs!FieldsName.ActualSize)

Open filename For Binary As #1 ' #1 is filehandler

Put #1, , tempBinary ' see put for inputting data into file

Close #1 ' close the binary file


Using that piece of code, you can convert back the binary data (store in database) into image file. But you need to know first, what the kind of data is store in the database whether it is bitmap, tiff, jpeg, gif or png.

It easy, right? Kind of. But it is only one solution out of many other solutions. So, if you want the easy one (that can convert up to 5800++ data into image file), you can use this. But as I said, there are many solutions out there. So, your choice.

Oh, I hope its helpful for you guys.

No comments: