Storing images as BLOB in db in VB.Net

Private Sub UploadedFiles_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadedFiles.DoubleClick

‘ Downloads the selected file and displays it in the picture box.

‘ Finds the unique id of the file.

Dim drv As DataRowView = CType(UploadedFiles.SelectedItem, DataRowView)
Dim selectedText As String = drv.Row(“Name”).ToString()
Dim id As Long=-1

‘ the id is stored in text. The structure is: id – FileName.

id = Long.Parse(selectedText.Substring(0,selectedText.IndexOf(” – “,0)).Trim())

Dim filename As String=Nothing
Dim up As TransferPictures = New TransferPictures()
Dim result As Byte() = up.DownloadFile(id,filename)
up = Nothing


‘ We cannot assign a byte array directly to an image.
‘ We use MemoryStream, an object that creates a file in memory
‘ and than we pass this to create the image object.

Dim ms As MemoryStream = New MemoryStream(result, 0, result.Length)
Dim im As Image = Image.FromStream(ms)
Picture.Image = im
Catch ee As Exception
MessageBox.Show(“An error has occured.\n” + ee.Message)
End Try
End Sub
End Class

1 Comment

  1. Sammie Asava says:

    This database is compatible with .NET, Silverlight, Windows Phone, Mono, Monodroid, and Monotouch:

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s