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

Try

‘ 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

Advertisements

One Comment on “Storing images as BLOB in db in VB.Net

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: