[ Team LiB ] |
Recipe 7.8 Displaying an Image from a Database in a Windows Forms ControlProblemYou need to display an image from a database in a Windows Forms control. SolutionRead the image into a byte array and load it directly into a PictureBox control with a MemoryStream. The sample code contains six event handlers:
The C# code is shown in Example 7-16. Example 7-16. File: DisplayDatabaseImageForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Data; using System.Data.SqlClient; private DataSet ds; private SqlDataAdapter da; private BindingManagerBase bm; // . . . private void DisplayDatabaseImageForm_Load(object sender, System.EventArgs e) { // Create the DataSet. ds = new DataSet( ); // Create the DataAdapter and retrieve the Employees table. String selectCommand = "SELECT EmployeeID, LastName, FirstName FROM Employees"; da = new SqlDataAdapter(selectCommand, ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.FillSchema(ds, SchemaType.Source, "Employees"); da.Fill(ds, "Employees"); // Bind several table fields to controls. employeeIdTextBox.DataBindings.Add("Text", ds, "Employees.EmployeeID"); lastNameTextBox.DataBindings.Add("Text", ds, "Employees.LastName"); firstNameTextBox.DataBindings.Add("Text", ds, "Employees.FirstName"); // Get the binding manager base for the Employees table. bm = BindingContext[ds, "Employees"]; // Update the image in response to each record reposition. bm.PositionChanged += new EventHandler(bm_PositionChanged); // Update the display for the first record. bm_PositionChanged(null, null); } private void bm_PositionChanged(Object sender, EventArgs e) { // Refresh the photo displayed when the current record changes. // Get the new EmployeeID using the BindingManager. int employeeId = (int)ds.Tables["Employees"].Rows[bm.Position]["EmployeeID"]; // Create a connection. SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings["Sql_ConnectString"]); // Create a command to retrieve the employee photo. String sqlText = "SELECT Photo FROM Employees WHERE EmployeeID=" + employeeId; SqlCommand cmd = new SqlCommand(sqlText, conn); // Retrieve the employee photo to a stream. conn.Open( ); Byte[] image = (Byte[])cmd.ExecuteScalar( );; MemoryStream ms = new MemoryStream(image); conn.Close( ); // Load the image into the PictureBox from the stream. photoPictureBox.Image = Image.FromStream(ms); ms.Close( ); } private void moveFirstButton_Click(object sender, System.EventArgs e) { bm.Position = 0; } private void movePreviousButton_Click(object sender, System.EventArgs e) { bm.Position -= 1; } private void moveNextButton_Click(object sender, System.EventArgs e) { bm.Position += 1; } private void moveLastButton_Click(object sender, System.EventArgs e) { bm.Position = bm.Count - 1; DiscussionThe Windows Forms PictureBox control displays bitmap, JPEG, metafile, or icon images. In the solution, the image stored as a BLOB in the database is retrieved into a Byte array, which is in turn copied into a System.IO.MemoryStream object. The static FromStream( ) method of the Image class creates an Image object that is loaded into the PictureBox.
|
[ Team LiB ] |