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.Load
Sets up the sample by filling a
DataTable
within aDataSet
with the Categories table from the Microsoft Access Northwind sample database. The CategoryID, CategoryName, and Description fields are bound toTextBox
controls. TheBindingManagerBase
is obtained for the Categories table in theDataSet
, a handler is attached to manage thePositionChanged
event, and that handler is called to position the display on the first record.BindingManagerBase.PositionChanged
Updates the
PictureBox
with the image for the current record. This event is raised when thePosition
property value changes.The CategoryID for the current record is obtained using the position information in the
BindingManagerBase
object. AConnection
object andCommand
object are created and used to retrieve thePicture
binary field for the category record into aByte
array. AMemoryStream
object is created and the image written into it from theByte
array using an offset to strip the OLE image header. The staticFromStream( )
method of theSystem.Drawing.Image
class is used to load the image into thePictureBox
from theMemoryStream
.- Move First
Button.Click
Sets the current record of the bound controls to the first record by setting ...
Get ADO.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.