Otis wrote:
in the datatable, if there's no value, it's likely the value is DBNull.Value, while in the entity the image field is returning Nothing (null).
I think this is your problem, not sure if a picture box can help with this in the sense that it can trap these situations and let you display a different picture or clear it ?
The application creates visitors badges which I construct in a PrintPreviewControl. I've just realised that the bound PictureBox is redundant so I've removed it! I have a dialog that now returns a byte array, ready to populate the .Photo attribute, which maps to the OLE Object field in the Access database.
I include the following code extracts for others' reference.
Private Sub btnImportPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportPhoto.Click
Dim importImageForm As New ImportImage(ImportImage.ImageFromByteArray(CType(bsPersons.Current, PersonEntity).Photo))
Try
importImageForm.ShowDialog()
If importImageForm.DialogResult = Windows.Forms.DialogResult.OK Then
'pbPreviewArea.Image = importImageForm.ImportedBitmap
CType(bsPersons.Current, PersonEntity).Photo = importImageForm.ImportedImageAsByteArray
PrintPreviewControl1.InvalidatePreview()
End If
Catch ex As Exception
HandleError(ex)
End Try
End Sub
'''<summary>
''' Get Image from a Byte Array (the format it is stored in database) to Image. If Byte Array is zero length return nothing.
'''</summary>
Public Shared Function ImageFromByteArray(ByVal imageAsByteArray As System.Byte()) As Image
If imageAsByteArray.Length <> 0 Then
Return Image.FromStream(New System.IO.MemoryStream(imageAsByteArray))
Else
Return Nothing
End If
End Function
'''<summary>
''' Convert ByteArray (the format that images are stored in the database) to an Image
'''</summary>
''' Convert Image to ByteArray for storing in BLOB.
Public Shared Function ByteArrayFromImage(ByVal img As Image) As Byte()
If Not img Is Nothing Then
Dim ms As New System.IO.MemoryStream
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Return ms.ToArray
Else
Return Nothing
End If
End Function
Thanks to everyone for their suggestions.
Graham