Show image in PictureBox from DB - vb.net

When I retrieve an object from my DB, I try to do this:
Dim pictureBytes As New MemoryStream(imagen.JugFir)
pictureBox2.Image = Image.FromStream(pictureBytes)
But I get an error:
'imagen.JugFir' is an Object and Image.FromStream() requires a Byte[].
How i can convert Object to Byte[] ?
Or is there any other way to load the picture?

Related

Display picture from access and show on form in vb.net

I want to display picture from access and show that on a form in vb.net
I have this for display information:
info.TextBox5.Text = DataGridView1.SelectedRows(0).Cells(5).Value
Now I tried something like this for pictures:
info.PictureBox1.Image = DataGridView1.SelectedRows(0).Cells(6).Value
But I got an error:
Can not associate the type of Object ' System.Byte []' to type '
System.Drawing.Image ' .
Can you help me?
dim str as string =DataGridView1.SelectedRows(0).Cells(5).Value
if str <> string.empty then
info.TextBox5.Text = str
end if
Use this:
Using ms As New MemoryStream(CType(DataGridView1.SelectedRows(0).Cells(6).Value, Byte()))
info.PictureBox1.Image = New Bitmap(ms)
End Using
Since you cannot simply assign a Byte() to an Image, you need to first create a Bitmap out of the data.
So what the above code does is:
Creates a new MemoryStream with the Byte() returned by DataGridView1.SelectedRows(0).Cells(6).Value.
Initializes a new Bitmap with the data in that stream, so that you can assign it to the Image property of your PictureBox.

Unable to cast object of type vb.net

Invalid cast exception was unhandled
The error is in "me.picturebox3.image = dt.rows(0).item("pic")
Unable to cast object of type system.byte[] to type system.drawing.image.
Dim dt as new datatable
Da.fill(dt)
Me.picturebox3.image = dt.rows(0).item("pic")
I am new in calling images from data table to picturebox. Help please..
Thanks
You need to find a way to construct an object of type System.Drawing.Image from the System.Byte() you currently have on hand.
Here's how you do it:
Dim bytes As Byte() = CType(dt.Rows(0).Item("pic"), Byte())
Dim ms As New MemoryStream(bytes)
Me.picturebox3.Image = Image.FromStream(ms)
This will work as long as your Byte() is in a format that can be handled out of the box, i.e. JPEG, BMP etc.

How to save picture in the database

I am using following code to save my data to database
Where does the statement to save a picture in the database fit?
Dim drNewRowMCQsAns As DataRow
drNewRowMCQsAns = DsResultSaveNow.tblResult.NewRow
drNewRowMCQsAns.Item("PaperID") = vrPaperIDInitialized
drNewRowMCQsAns.Item("StudentID") = vrStudentID
drNewRowMCQsAns.Item("StudentName") = vrStudentName
DsResultSaveNow.tblResult.Rows.Add(drNewRowMCQsAns)
taResultSaveNow.Update(DsResultSaveNow.tblResult)
I have image field in the database but how to save an image?
Thanks
Well, the image data is simply a byte array
Dim imageData as Byte()
Load your image into the byte array from where ever you are getting it from and set it just like the other properties
drNewRowMCQsAns.Item("ImageData") = imageData
Loading image into array:
From file:
imageData = IO.File.ReadAllBytes("c:\filename.jpg")
From bitmap:
Dim bitmap As New System.Drawing.Bitmap("c:\filename.jpg")
Dim tempMemStream As New IO.MemoryStream
bitmap.Save(tempMemStream, System.Drawing.Imaging.ImageFormat.Jpeg)
imageData = tempMemStream.ToArray()

Deserialize a Digital Persona template in VB.net

Reading binary data out of the database, and I need to convert it back into a Digital Persona fingerprint template. I'm not familiar with serialization and deserialization, so I could use a bit of help. Here's what I tried:
Dim rsBioData As SqlDataReader = SQL.ExecuteReader
Dim byteTemplate As Byte
Dim memStreamTemplate As MemoryStream
If rsBioData.HasRows Then
While rsBioData.Read
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template = DirectCast(template.DeSerialize(memStreamTemplate), DPFP.Template)
End While
End If
rsBioData.Close()
I receive an error that template.DeSerialize(memStreamTemplate) does not create a value.
For kicks, here's how I serialized the object to place it into the database. I assume this part is working, since the binary data shows up in SQL server--just can't read it back out to see.
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
SQL.Parameters.AddWithValue("biometricData", serializedTemplate)
Thanks
Here's how I was finally able to do it. I was SO close the first time around.
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template.DeSerialize(memStreamTemplate)

Image field on ReportViewer

Anybody tried to display image field (image/byte array) data type on reportviewer.
Regards,
Peter
Yes. ReportViewer requires a Base64 Image encoding in order to display the image properly.
If your image is in a Byte array, it will need to be converted to Base64:
Public Function ConvertImageToBase64String(ByVal img As Image) As String
Dim output As String = ""
Dim outputArray() As Byte
Dim stream As New MemoryStream
img.Save(stream, Drawing.Imaging.ImageFormat.Bmp)
outputArray = stream.ToArray()
stream.Close()
output = Convert.ToBase64String(outputArray)
Return output
End Function
I have with an image data type in SQL Server. Works fine with SSRS 2005 and 2008.