The PictureBox report item is used to display images on a report and it supports only image formats supported by GDI+ (BMP, GIF, JPEG, EXIF, PNG and TIFF).
Its value can be an expression that contains binary image data or it can be a URI (local path or Url).
When a Value property is assigned a string that is not an expression (doesn't start with "=") the value is assumed to be an URI. Click the ellipsis for this property to
browse for an image file on your hard drive.
This screenshot shows a PictureBox report item on a report at design-time (note the image thumbnail in the upper left corner).
Data Binding
Binding image data to a PictureBox report item is straight-forward. If you’re using the designer in Visual
Studio and have connected to a data source, you can simply drag the image field from the Data Explorer
window onto your report. The designer is smart enough to know
what kind of report item to associate with each field based on its data type.
Alternatively, one can drag a PictureBox item onto the design surface and use an expression to set its Value
property to an existing field from the data source.
While the methods above are quickest to get an image onto your report, one doesn’t have to bind directly to a
database field with an image column. The type of the PictureBox.Value property is Object
which allows for versatile data binding. The Value property accepts objects of type Image and strings.
Binding to a Binary Image
You can directly assign a reference to an Image
to the Value property of a PictureBox:
CopyC#
using System.Drawing;
Image image1 = Image.FromFile(@"C:\MyPictures\MyPicture.jpg");
this.pictureBox1.Value = image1;
Image image2 = Image.FromStream(imageStream);
this.pictureBox2.Value = image2;
CopyVB.NET
Imports System.Drawing
Dim image1 As Image = Image.FromFile("C:\MyPictures\MyPicture.jpg")
Me.PictureBox1.Value = image1
Dim image2 As Image = Image.FromStream(imageStream)
Me.PictureBox2.Value = image2While in design-time, if you click the ellipsis of the Value property of a selected PictureBox, you will be presented
with a dialog to choose the desired image. After you have selected the image, the Designer will automatically store it in
the resources file for the report (.resx) and add a line of code to the InitializeComponent method that obtains a reference
to the image stored in the resources and assigns it to the Value property of the PictureBox:
CopyC#
this.pictureBox1.Value = ((object)(resources.GetObject("pictureBox1.Value")));When the database field contains relative path (be it file path or uri) you can use User Functions
to specify the correct path to the image:
CopyC#
public static Image LoadImage(string imageLocation)
{
string absoluteLocation = "C:\\" + imageLocation;
return Image.FromFile(absoluteLocation);
}
CopyVB.NET
Public Shared Function LoadImage(imageLocation As String) As Image
Dim absoluteLocation As String = "C:\" + imageLocation
Return Image.FromFile(absoluteLocation)
End Function
and set the Value of the PictureBox item to the correct expression: =LoadImage(Fields.YourImagePathColumn).
Binding to Expression/URI
Alternatively you can assign a string value to the Value property of a PictureBox. This string value can be either an item binding expression or an URI. Both absolute and relative URIs are supported. When binding to a data field with an expression, the Value property will accept both binary and string data fields. In other words, your data source column may store the image directly or store an URI pointing to the image:
CopyC#
this.pictureBox1.Value = "=Fields.MyImageBinary";
this.pictureBox2.Value = "=Fields.MyImageURI";
this.pictureBox3.Value = @"C:\MyPictures\MyPicture.png";
this.pictureBox4.Value = @".\images\MyPicture.png";
this.pictureBox5.Value = "http://www.mysite.com/images/img1.gif";
this.pictureBox6.Value = "/images/img1.gif";
CopyVB.NET
Me.PictureBox1.Value = "=Fields.MyImageBinary"
Me.PictureBox2.Value = "=Fields.MyImageURI"
Me.PictureBox3.Value = "C:\MyPictures\MyPicture.png"
Me.PictureBox4.Value = ".\images\MyPicture.png"
Me.PictureBox5.Value = "http://www.mysite.com/images/img1.gif"
Me.PictureBox6.Value = "/images/img1.gif"
Clipping and positioning
The clipping and positioning of an image in the display area of a PictureBox item is controlled by its
Sizing property.
Sizing Property modes:
| Name | Description |
|---|
| AutoSize | The PictureBox item size is adjusted to that of the image it contains. |
| Center | The image is displayed in the center of the PictureBox item. If the image is larger than the PictureBox item, the outside edges are clipped. |
| Normal | The image is placed in the upper-left corner of the PictureBox item. The image is clipped if it's larger than the PictureBox item which contains it. |
| Stretch | The image within the PictureBox item is stretched or shrunk as appropriate to fit the size of the PictureBox item. |
| ScaleProportional | The image is sized proportionally (without clipping), so that it's best fitted to the PictureBox item.
If the height and width ratio of the PictureBox item is the same as the image's ratio it will be resized
to exactly fit into the PictureBox item. Otherwise the closest fitting side (height or width) of the
image will be sized to the item and the other side (height or width) of the image sized proportionally (leaving empty space). |
See Also