Take the following steps to create and run a program that shows how to add
data binding to a RasterImageViewer control.
Note: You should first create a database file using MS Access that has at least one table, and the
table name should be "Example". This table should include at least one file using the
"Image" name and the "OLE Object" data type.
Start Visual Studio .NET. Choose File->New->Project… from the menu. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" in the Templates List. Type the project name as "DataBindingTutorial" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32 " folder and select the following DLLs: Leadtools.dll Leadtools.Codecs.dll Leadtools.WinForms.dll Leadtools.WinForms.CommonDialogs.File.dll
Click Select and then clickOK to add the above DLLs to the application. Drag an OleDbDataAdapter control from the Data group in the Toolbox and drop it on Form1. When the Data Adapter Configuration Wizard dialog appears, perform the following steps: Click New Connection..., and the Add Connection dialog will appear. Click Change..., and the Change Data Source dialog will appear. From Data source list select Microsoft Access Database File and click OK. In the Add Connection dialog click Browse....and browse to the place where you created the database file and select the file. Then clickOpen and in the Add Connection dialog click OK. In the Data Adapter Configuration Wizard dialog click Next. When the message box appears, click No. Select Use SQL statments and click Next. Click Query Build... and then click Next. In the Add Table dialog on the Tablestab select Example.and click Add and then click Close. In the Query Builder dialog from Example check * (All Columns) item then click OK. In the Data Adapter Configuration Wizard dialog click Next and then click Finish. Notice that the oleDbConnection1 control has been created. In the design view right-click on oleDbDataAdapter1 and click on Generate DataSet.. item. In Generate Dataset dialog click OK. Noticee that a dataSet11 icon has been created.
Drag and drop a BindingSource control onto Form1 from the Data group. Now change the following properties of bindingSource1: Property Value DataSource dataSet11 DataMember Example Drag and drop a BindingNavigator control onto Form1 from the Data group. Now change the following properties of bindingNavigator1: Property Value BindingSource bindingSource1 DataMember Example Drag a Panel control from the Containers group in the Toolbox and drop it onto Form1. Now change the following properties of panel1: Property Value Name _pnlControls Dock Left Drag and drop a button in _pnlControls. Now change the following properties of button1: Property Value Name _btnLoad Text Load Drag and drop a button in _pnlControls. Now change the following properties of button2: Property Value Name _btnSubmit Text Submit Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file: [Visual Basic]
[C#]Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.WinForms Imports Leadtools.WinForms.CommonDialogs.File
using Leadtools; using Leadtools.Codecs; using Leadtools.WinForms; using Leadtools.WinForms.CommonDialogs.File;
Add the following private variables to the Form1 class: [Visual Basic]
[C#]private _viewer As RasterImageViewer private _codecs As RasterCodecs
private RasterImageViewer _viewer; private RasterCodecs _codecs;
Add an event handler to the Form1 Load event and add the following code: [Visual Basic]
[C#]Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) oleDbDataAdapter1.Fill(dataSet11) ' Initialize a new RasterCodecs object RasterCodecs.Startup() _codecs = New RasterCodecs() _viewer = New RasterImageViewer() _viewer.BackColor = Color.Bisque _viewer.Dock = DockStyle.Fill Controls.Add(_viewer) _viewer.BringToFront() _viewer.BindingRasterCodecs = _codecs _viewer.DataBindings.Add(New System.Windows.Forms.Binding("BindingData", bindingSource1, "Image", True)) End Sub
private void Form1_Load(object sender, EventArgs e) { oleDbDataAdapter1.Fill(dataSet11); // Initialize a new RasterCodecs object RasterCodecs.Startup(); _codecs = new RasterCodecs(); _viewer = new RasterImageViewer(); _viewer.BackColor = Color.Bisque; _viewer.Dock = DockStyle.Fill; Controls.Add(_viewer); _viewer.BringToFront(); _viewer.BindingRasterCodecs = _codecs; _viewer.DataBindings.Add(new System.Windows.Forms.Binding("BindingData", bindingSource1, "Image", true)); }
Add the following code to the Form1 class: [Visual Basic]
[C#]Private Sub _btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs) Dim ofd As RasterOpenDialog = New RasterOpenDialog(_codecs) ofd.DereferenceLinks = True ofd.CheckFileExists = False ofd.CheckPathExists = True ofd.EnableSizing = False ofd.LoadFileImage = False ofd.LoadOptions = False ofd.LoadRotated = False ofd.LoadCompressed = False ofd.Multiselect = False ofd.ShowGeneralOptions = False ofd.ShowLoadCompressed = False ofd.ShowLoadOptions = False ofd.ShowLoadRotated = False ofd.ShowMultipage = False ofd.ShowPreview = True ofd.ShowProgressive = False ofd.ShowRasterOptions = False ofd.ShowTotalPages = False ofd.ShowDeletePage = False ofd.ShowFileInformation = True ofd.UseFileStamptoPreview = False ofd.PreviewWindowVisible = True ofd.Title = "LEADTOOLS Open Dialog" If ofd.ShowDialog(Me) = DialogResult.OK Then _viewer.Image = _codecs.Load(ofd.OpenedFileData(0).Name, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1) End If End Sub
private void _btnLoad_Click(object sender, EventArgs e) { RasterOpenDialog ofd = new RasterOpenDialog(_codecs); ofd.DereferenceLinks = true; ofd.CheckFileExists = false; ofd.CheckPathExists = true; ofd.EnableSizing = false; ofd.LoadFileImage = false; ofd.LoadOptions = false; ofd.LoadRotated = false; ofd.LoadCompressed = false; ofd.Multiselect = false; ofd.ShowGeneralOptions = false; ofd.ShowLoadCompressed = false; ofd.ShowLoadOptions = false; ofd.ShowLoadRotated = false; ofd.ShowMultipage = false; ofd.ShowPreview = true; ofd.ShowProgressive = false; ofd.ShowRasterOptions = false; ofd.ShowTotalPages = false; ofd.ShowDeletePage = false; ofd.ShowFileInformation = true; ofd.UseFileStamptoPreview = false; ofd.PreviewWindowVisible = true; ofd.Title = "LEADTOOLS Open Dialog"; if (ofd.ShowDialog(this) == DialogResult.OK) { _viewer.Image = _codecs.Load(ofd.OpenedFileData[0].Name, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); } }
Add the following code to the Form1 class: [Visual Basic]
[C#]Private Sub _btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) bindingSource1.EndEdit() oleDbDataAdapter1.Update(dataSet11) End Sub
private void _btnSubmit_Click(object sender, EventArgs e) { bindingSource1.EndEdit(); oleDbDataAdapter1.Update(dataSet11); }
Build, and Run the program to test it. Save this project to use for testing other code samples.