Loading a Windows PictureBox with Images Stored by Access as OLE Objects
Số trang: 5
Loại file: pdf
Dung lượng: 16.75 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 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
Nội dung trích xuất từ tài liệu:
Loading a Windows PictureBox with Images Stored by Access as OLE Objects[ Team LiB ]Recipe 7.13 Loading a Windows PictureBox with Images Stored by Access as OLEObjectsProblemYou need to display images from a Microsoft Access database in a PictureBox control.SolutionStrip 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 a DataSet with the Categories table from the Microsoft Access Northwind sample database. The CategoryID, CategoryName, and Description fields are bound to TextBox controls. The BindingManagerBase is obtained for the Categories table in the DataSet, a handler is attached to manage the PositionChanged 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 the Position property value changes. The CategoryID for the current record is obtained using the position information in the BindingManagerBase object. A Connection object and Command object are created and used to retrieve the Picture binary field for the category record into a Byte array. A MemoryStream object is created and the image written into it from the Byte array using an offset to strip the OLE image header. The static FromStream( ) method of the System.Drawing.Image class is used to load the image into the PictureBox from the MemoryStream.Move First Button.Click Sets the current record of the bound controls to the first record by setting the Position property of the BindingManagerBase object to 0.Move Previous Button.Click Sets the current record of the bound controls to the previous record by decrementing the Position property of the BindingManagerBase object by 1.Move Next Button.Click Sets the current record of the bound controls to the next record by incrementing the Position property of the BindingManagerBase object by 1.Move Last Button.Click Sets the current record of the bound controls to the last record by setting the Position property of the BindingManagerBase object to the total number of records, as returned by the Count property, minus 1.The C# code is shown in Example 7-29.Example 7-29. File: DisplayMsAccessImageForm.csusing System;using System.Configuration;using System.Drawing;using System.Windows.Forms;using System.IO;using System.Data;using System.Data.OleDb;private const int MSACCESSIMAGEOFFSET = 78;private DataSet ds;private OleDbDataAdapter da;private BindingManagerBase bm;// . . .private void DisplayMsAccessImageForm_Load(object sender, System.EventArgs e){ // Create the DataSet. ds = new DataSet( ); // Create the DataAdapter and retrieve the Categories table. String selectCommand = SELECT CategoryID, CategoryName, Description FROM Categories; da = new OleDbDataAdapter(selectCommand, ConfigurationSettings.AppSettings[MsAccess_ConnectString]); da.FillSchema(ds, SchemaType.Source, Categories); da.Fill(ds, Categories); // Bind table fields to controls. categoryIdTextBox.DataBindings.Add(Text, ds, Categories.CategoryID); categoryNameTextBox.DataBindings.Add(Text, ds, Categories.CategoryName); descriptionTextBox.DataBindings.Add(Text, ds, Categories.Description); // Get the binding manager base for the parent table. bm = BindingContext[ds, Categories]; // 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 CategoryID using the BindingManager. int categoryId = (int)ds.Tables[Categories].Rows[bm.Position][CategoryID]; // Create a connection. OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[MsAccess_ConnectString]); // Create a command to retrieve the category photo. String sqlText = SELECT Picture FROM Categories WHERE CategoryID= + categoryId; OleDbCommand cmd = new OleDbCommand(sqlText, conn); // Retrieve the image from the database. conn.Open( ); Byte[] image = (Byte[])cmd.ExecuteScalar( ); // Write to a stream removing the image header. MemoryStream ms = new MemoryStream( ); ms.Write(image, MSACCESSIMAGEOFFSET, image.Length - MSACCESSIMAGEOFFSET); conn.Close( ); // Load the image into the PictureBox from the stream. picturePictureBox.Image = Image.FromStream(ms); ms.Close( );}p ...
Nội dung trích xuất từ tài liệu:
Loading a Windows PictureBox with Images Stored by Access as OLE Objects[ Team LiB ]Recipe 7.13 Loading a Windows PictureBox with Images Stored by Access as OLEObjectsProblemYou need to display images from a Microsoft Access database in a PictureBox control.SolutionStrip 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 a DataSet with the Categories table from the Microsoft Access Northwind sample database. The CategoryID, CategoryName, and Description fields are bound to TextBox controls. The BindingManagerBase is obtained for the Categories table in the DataSet, a handler is attached to manage the PositionChanged 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 the Position property value changes. The CategoryID for the current record is obtained using the position information in the BindingManagerBase object. A Connection object and Command object are created and used to retrieve the Picture binary field for the category record into a Byte array. A MemoryStream object is created and the image written into it from the Byte array using an offset to strip the OLE image header. The static FromStream( ) method of the System.Drawing.Image class is used to load the image into the PictureBox from the MemoryStream.Move First Button.Click Sets the current record of the bound controls to the first record by setting the Position property of the BindingManagerBase object to 0.Move Previous Button.Click Sets the current record of the bound controls to the previous record by decrementing the Position property of the BindingManagerBase object by 1.Move Next Button.Click Sets the current record of the bound controls to the next record by incrementing the Position property of the BindingManagerBase object by 1.Move Last Button.Click Sets the current record of the bound controls to the last record by setting the Position property of the BindingManagerBase object to the total number of records, as returned by the Count property, minus 1.The C# code is shown in Example 7-29.Example 7-29. File: DisplayMsAccessImageForm.csusing System;using System.Configuration;using System.Drawing;using System.Windows.Forms;using System.IO;using System.Data;using System.Data.OleDb;private const int MSACCESSIMAGEOFFSET = 78;private DataSet ds;private OleDbDataAdapter da;private BindingManagerBase bm;// . . .private void DisplayMsAccessImageForm_Load(object sender, System.EventArgs e){ // Create the DataSet. ds = new DataSet( ); // Create the DataAdapter and retrieve the Categories table. String selectCommand = SELECT CategoryID, CategoryName, Description FROM Categories; da = new OleDbDataAdapter(selectCommand, ConfigurationSettings.AppSettings[MsAccess_ConnectString]); da.FillSchema(ds, SchemaType.Source, Categories); da.Fill(ds, Categories); // Bind table fields to controls. categoryIdTextBox.DataBindings.Add(Text, ds, Categories.CategoryID); categoryNameTextBox.DataBindings.Add(Text, ds, Categories.CategoryName); descriptionTextBox.DataBindings.Add(Text, ds, Categories.Description); // Get the binding manager base for the parent table. bm = BindingContext[ds, Categories]; // 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 CategoryID using the BindingManager. int categoryId = (int)ds.Tables[Categories].Rows[bm.Position][CategoryID]; // Create a connection. OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[MsAccess_ConnectString]); // Create a command to retrieve the category photo. String sqlText = SELECT Picture FROM Categories WHERE CategoryID= + categoryId; OleDbCommand cmd = new OleDbCommand(sqlText, conn); // Retrieve the image from the database. conn.Open( ); Byte[] image = (Byte[])cmd.ExecuteScalar( ); // Write to a stream removing the image header. MemoryStream ms = new MemoryStream( ); ms.Write(image, MSACCESSIMAGEOFFSET, image.Length - MSACCESSIMAGEOFFSET); conn.Close( ); // Load the image into the PictureBox from the stream. picturePictureBox.Image = Image.FromStream(ms); ms.Close( );}p ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Displaying an Image from a Database in a Web Forms ControlGợi ý tài liệu liên quan:
-
52 trang 417 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 301 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 287 0 0 -
74 trang 283 0 0
-
96 trang 283 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 268 1 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 260 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 252 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 251 0 0