Re: Windows form and images
I think that you'd just add another field with the image's URL. Then set VB.NET to get the data, and use a picturebox, then call the image location as that field's contents ;)
Hope that helps.
Re: Windows form and images
Depends on how you want to store the data. Microsoft Access (And I guess any database) allows you to store binary data in a database, which is what I do, although you can also just store a path if you wish.
If you wish to store the actual image, you need to set the field up as an OLE Object field, then in your code, you'll need to convert the image data to/from a data type that the database can read/write.
To store an image, it's simply a matter of converting the image into a Byte array.
VB Code:
'
'Visual Basic 2005
Private Shared Function ToByteArray(ByVal data As Image) As Byte()
If data IsNot Nothing Then
Dim dataStream As New IO.MemoryStream()
Using dataStream
data.Save(dataStream, data.RawFormat)
End Using
Return dataStream.GetBuffer
Else
Return Nothing
End If
End Function
'
'Visual Basic 2003
Private Shared Function ToByteArray(ByVal data As Image) As Byte()
If Not (data Is Nothing) Then
Dim dataStream As New IO.MemoryStream()
data.Save(dataStream, data.RawFormat)
dataStream.Close()
Return dataStream.GetBuffer
Else
Return Nothing
End If
End Function
then after you've done that, store that in the column for the record:
VB Code:
'
'Visual Basic 2005
Dim myConnection As New OleDbConnection(...)
myConnection.Open()
Using myConnection
Dim myCommand As New OleDbCommand(... , myConnection)
With myCommand.Parameters
If [i]imageObject[/i] IsNot Nothing Then
.AddWithValue("?", ToByteArray([i]imageObject[/i]))
Else
.AddWithValue("?", DBNull.Value)
End If
End With
myCommand.ExecuteNonQuery()
End Using
'
'Visual Basic 2003
Dim myConnection As New OleDbConnection(...)
myConnection.Open()
Dim myCommand As New OleDbCommand(... , myConnection)
With myCommand.Parameters
If Not ([i]imageObject[/i] Is Nothing) Then
.AddWithValue("?", ToByteArray([i]imageObject[/i]))
Else
.AddWithValue("?", DBNull.Value)
End If
End With
myCommand.ExecuteNonQuery()
myConnection.Close()
Now that that's complete, we have to get the data back as an Image.
VB Code:
'
'Visual Basic 2005
Private Shared Function ToImage(ByVal data As Byte()) As Image
Dim dataStream As New IO.MemoryStream(data)
Using dataStream
Return Image.FromStream(dataStream)
End Using
End Function
'
'Visual Basic 2003
Private Shared Function ToImage(ByVal data As Byte()) As Image
Dim dataStream As New IO.MemoryStream(data)
Dim ConvertedImage As Image
ConvertedImage = Image.FromStream(dataStream)
dataStream.Close()
Return ConvertedImage
End Function
and when you get it from the database using a select statement
VB Code:
'
'Visual Basic 2005
Dim myConnection As New OleDbConnection(...)
myConnection.Open()
Dim myCommand As New OleDbCommand(... , myConnection)
Dim myDataReader As OleDbDataReader = myCommand.ExecuteReader
Using myConnection
Using myDataReader
myDataReader.Read()
If myDataReader("[i]columnName[/i]") IsNot DBNull.Value Then
[i]variable[/i] = ToImage(CType(myDataReader("[i]columnName[/i]"), Byte()))
End If
End Using
End Using
'
'Visual Basic 2003
Dim myConnection As New OleDbConnection(...)
myConnection.Open()
Dim myCommand As New OleDbCommand(... , myConnection)
Dim myDataReader As OleDbDataReader = myCommand.ExecuteReader
myDataReader.Read()
If Not (myDataReader("[i]columnName[/i]") Is DBNull.Value) Then
[i]variable[/i] = ToImage(CType(myDataReader("[i]columnName[/i]"), Byte()))
End If
myDataReader.Close()
myConnection.Close()
Obviously you need to replace the italicised items with proper values, however this should work.
There is however one bug in it, which is more to do with the way Windows handles images using GDI+, when I find out more about it, I'll let you know, but I think it has something to do with loading and then saving the image in the same session.
Another note, I did this is Visual Basic 2005, hence all the VB2005 code, I've taken out the obvious syntax changes that were introduced, so hopefully the remaining functionality should work as expected in Visual Basic .NET 2003.