7.13. Loading a Windows PictureBox with Images Stored by Access as OLE Objects
Problem
You need to display images from a
Microsoft
Access database in a PictureBox control.
Solution
Strip the OLE image header that Microsoft Access adds to the image.
The sample code contains six event handlers:
Form.LoadSets up the sample by filling a
DataTablewithin aDataSetwith the Categories table from the Microsoft Access Northwind sample database. The CategoryID, CategoryName, and Description fields are bound toTextBoxcontrols. TheBindingManagerBaseis obtained for the Categories table in theDataSet, a handler is attached to manage thePositionChangedevent, and that handler is called to position the display on the first record.BindingManagerBase.PositionChangedUpdates the
PictureBoxwith the image for the current record. This event is raised when thePositionproperty value changes.The CategoryID for the current record is obtained using the position information in the
BindingManagerBaseobject. AConnectionobject andCommandobject are created and used to retrieve thePicturebinary field for the category record into aBytearray. AMemoryStreamobject is created and the image written into it from theBytearray using an offset to strip the OLE image header. The staticFromStream( )method of theSystem.Drawing.Imageclass is used to load the image into thePictureBoxfrom theMemoryStream.- Move First
Button.Click Sets the current record of the bound controls to the first record by setting ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access