Binding a PictureBox to an Access OLEObject database field

Posts   
 
    
like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 04-Nov-2009 11:38:35   

I've been messing with this for hours, so I thought I'd ask the experts.

I have a pictureBox bound to an OLE Object database field. I can update and display images correctly, they get read from and written to the database. However I have 2 issues:

  1. As I scroll through my records If there is no Image stored in the database then the pictureBox continues to show the image from the previous record unless I clear it manually with:
    
If currPerson.Photo.Length = 0 Then
        pbPreviewArea.Image = Nothing
End If

This works... but is there a better solution?

  1. If I want to remove the Image from the database I do:

      pbPreviewArea.Image = Nothing

but this doesn't trigger an update to database. Stepping through the code I can see that {collection}.ContainsDirtyContents = False after I have set "pbPreviewArea.Image = Nothing".

Any helpful hints greatfully received. VB / Winforms/ VS2008 / Access 2003 / LLBLGenPro 2.6

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 04-Nov-2009 11:41:30   

like2175 wrote:

I've been messing with this for hours, so I thought I'd ask the experts.

I have a pictureBox bound to an OLE Object database field. I can update and display images correctly, they get read from and written to the database. However I have 2 issues:

  1. As I scroll through my records If there is no Image stored in the database then the pictureBox continues to show the image from the previous record unless I clear it manually with:
    
If currPerson.Photo.Length = 0 Then
        pbPreviewArea.Image = Nothing
End If

This works... but is there a better solution?

  1. If I want to remove the Image from the database I do:

      pbPreviewArea.Image = Nothing

but this doesn't trigger an update to database. Stepping through the code I can see that {collection}.ContainsDirtyContents = False after I have set "pbPreviewArea.Image = Nothing".

My Picture box is declared as follows:


'pbPreviewArea
    '
    Me.pbPreviewArea.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.pbPreviewArea.BackColor = System.Drawing.Color.Yellow
    Me.pbPreviewArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.pbPreviewArea.DataBindings.Add(New System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, """"""))
    Me.pbPreviewArea.ErrorImage = CType(resources.GetObject("pbPreviewArea.ErrorImage"), System.Drawing.Image)
    Me.pbPreviewArea.InitialImage = CType(resources.GetObject("pbPreviewArea.InitialImage"), System.Drawing.Image)
    Me.pbPreviewArea.Location = New System.Drawing.Point(217, 177)
    Me.pbPreviewArea.Name = "pbPreviewArea"
    Me.pbPreviewArea.Size = New System.Drawing.Size(45, 39)
    Me.pbPreviewArea.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
    Me.pbPreviewArea.TabIndex = 24
    Me.pbPreviewArea.TabStop = False

Any helpful hints greatfully received. VB / Winforms/ VS2008 / Access 2003 / LLBLGenPro 2.6

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-Nov-2009 11:48:45   

pbPreviewArea.Image = Nothing

Is pbPreviewArea an entity? Is Image an entity?

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 04-Nov-2009 12:00:00   

Walaa wrote:

pbPreviewArea.Image = Nothing

Is pbPreviewArea an entity? Is Image an entity?

pbPreviewArea is the PictureBox control. Image is used to get or set the image http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-Nov-2009 15:23:45   

Would you please post the binding code of pbPreviewArea.Image.

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 04-Nov-2009 15:32:42   

Walaa wrote:

Would you please post the binding code of pbPreviewArea.Image.

In the .Designer.vb I have:


 Me.pbPreviewArea.DataBindings.Add(New System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, """"""))


I've attached the IDE from VS2008 showing the PictureBox in place. I have bound it through '(DataBindings) - Image' on the Properties pane shown.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-Nov-2009 17:08:35   

Please change the update mode from OnValidation to OnPropertyChanged.

Me.pbPreviewArea.DataBindings.Add(new System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 10-Nov-2009 15:01:16   

Walaa wrote:

Please change the update mode from OnValidation to OnPropertyChanged.

Me.pbPreviewArea.DataBindings.Add(new System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

I'm afraid that hasn't helped.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 10-Nov-2009 22:23:11   

Do you get the same behaviour if you bind directly to a datatable rather than an LLBLGen entity ?

Just trying to determing if the issue is with our code...

Matt

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 11-Nov-2009 12:26:16   

MTrinder wrote:

Do you get the same behaviour if you bind directly to a datatable rather than an LLBLGen entity ?

Just trying to determing if the issue is with our code...

Matt

Binding to a DataSet rather than an LLBLGenCollection it works.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 11-Nov-2009 13:36:30   

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 ?

Frans Bouma | Lead developer LLBLGen Pro
like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 11-Nov-2009 16:47:34   

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