Bind an Image Field to a PictureBox

Problem

You want to bind a PictureBox to a field that contains binary image data.

Solution

React to the Format data-binding event, and convert the binary data to an Image object.

Discussion

You can use data binding to bind any property from almost any control to a database field, including the Image property from the PictureBox control. However, the Image property requires an Image object—not just a byte array. Therefore, you need to perform some conversion to translate the binary data into an Image object.

You can use data-binding events to execute this conversion logic automatically. All you need to do is react to the Format event (first introduced in recipe Format Data-Bound Fields) and create a new Image object ...

Get Microsoft® Visual Basic® .NET Programmer's 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.