VB .....

Toitjie

Expert Member
Joined
Apr 6, 2008
Messages
1,786
Reaction score
64
Location
PTA/JHB
ok, heres the problem

i got a database with an image stored in it right... and other data...

i retrieve all the data from the database but i cant get the image to work.

if any1 can help on this, ill b happy.

thanx in advanced :confused::confused:
 
You need to know what format the image is in, how many pixels across by how many pixels down, stuff like that.

Are you perhaps simply assuming that the picture is a JPG stored in a binary field in the DB?

Perhaps it's not a JPG.

Try to write the database contents of that picture to a file, and then try to get the file to show as a picture using various picture viewing software, once you have that working, then you can go ahead and process the pictures.

or ...

Perhaps you don't know how to read a binary database field?

:confused:
 
Rebuilding images can be tricky. All the headers have to be 100% or it won't work.

I take cover when I see anything involving VB....so I can't help on anything specific.
 
What database and what field data type are you using? And isn't this a little off topic? :p

Edit: Or ON topic? :confused:
 
Why store the image in itself in the DB in the first place? Why not just store a URL to the image and save the image somewhere in a folder? And as sn3rd asked, what DB and field type are you using?
 
I don't know what database you are using, but I know SQL is making use of an Image type. The SqlDbType Image translates to a byte array in .Net as does the SqlDbType Binary.

I am assuming you are using VB.Net so here is the code:

''' <summary>
''' Convert Image object to byte()
''' </summary>
Public Shared Function GetImageBytes(ByVal imgObject As Image) As Byte()
Dim imageStream As New MemoryStream
imgObject.Save(imageStream, ImageFormat.Jpeg)
Dim imageBytes As Byte() = New Byte(imageStream.Length - 1) {}
imageStream.Position = 0
imageStream.Read(imageBytes, 0, CInt(imageStream.Length))
Return imageBytes
End Function

''' <summary>
''' Convert a byte() to an Image object
''' </summary>
Public Shared Function ImageFromBytes(ByVal ResPhoto As Byte()) As Image
Dim image As Image = Nothing
Try
Dim converter As New ImageConverter
If (ResPhoto.Length > 0) Then
image = DirectCast(converter.ConvertFrom(ResPhoto), Image)
End If
Catch exception As Exception
Throw exception
End Try
Return image
End Function
Hope it helps!
 
If you would be reading the image from a file on the file system:

From a File to a byte array:

Public Shared Function GetPhotoBytes(ByVal FilePath As String) As Byte()
Dim input As FileStream = Nothing
Dim reader As BinaryReader = Nothing
Dim buffer As Byte()
Try
Try
input = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
reader = New BinaryReader(input)
buffer = reader.ReadBytes(CInt(input.Length))
reader.Close
input.Close
Catch exception As Exception
Throw exception
End Try
Finally
input.Dispose
reader.Close
End Try
Return buffer
End Function

From a file Directly to a Image, you would combine the provious stated methods but I did the following:

Public Shared Function ImageFromFile(ByVal imageFile As String) As Image
Return ImageHelpers.ImageFromBytes(ImageHelpers.GetPhotoBytes(imageFile))
End Function
 
Just keep one thing in mind. If you are converting from a .Net Image object to a byte array you need to specify the image format. I am using only jpeg, but if you are using other types you would need to customize the method to allow for that also.

Hope it helped in some way.
 
Just keep one thing in mind. If you are converting from a .Net Image object to a byte array you need to specify the image format. I am using only jpeg, but if you are using other types you would need to customize the method to allow for that also.

Hope it helped in some way.
...and store in the database what Image type is being used (JPG,BMP,PNG,TIF,etc)

EDIT: As mention earlier in the thread (I think), the bytes in the image depend heavily on the content headers.

EDIT 2: The .NET framework has some powerful Image handling libraries. Make the most of them!
 
Top
Sign up to the MyBroadband newsletter
X