Add Data Binding to ImageViewer Control

Take the following steps to create and run a program that shows how to add data binding to a ImageViewer 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 column using the "Image" name and the "OLE Object" data type.

  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.
  4. Type the project name as "DataBindingTutorial" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
  5. 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 "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Controls.WinForms.dll
    • Leadtools.WinForms.CommonDialogs.File.dll
  6. Click Select and then click OK to add the above DLLs to the application.

  7. 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 click Open 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 Builder... and then click Next.
    • In the Add Table dialog on the Tables tab 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.
  8. In the design view right-click on oleDbDataAdapter1 and click on Generate DataSet... menu item.

  9. In Generate Dataset dialog click OK. Noticee that a dataSet11 icon has been created.
  10. 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
  1. Drag and drop a BindingNavigator control onto Form1 from the Data group. Now change the following properties of bindingNavigator1:
Property Value
BindingSource bindingSource1
  1. 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
  1. Drag and drop a button in _pnlControls. Now change the following properties of button1:
Property Value
Name _btnLoad
Text Load
  1. Drag and drop a button in _pnlControls. Now change the following properties of button2:
Property Value
Name _btnSubmit
Text Submit
  1. 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:

    C#
    VB
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.Controls.WinForms 
    Imports Leadtools.WinForms.CommonDialogs.File 
                     
         
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Controls.WinForms; 
    using Leadtools.WinForms.CommonDialogs.File; 
                     
         

  2. Add the following private variables to the Form1 class:

    C#
    VB
    private _viewer As ImageViewer 
    private _codecs As RasterCodecs 
                     
         
    private ImageViewer _viewer; 
    private RasterCodecs _codecs; 
                     
         

  3. Add an event handler to the Form1 Load event and add the following code:

    C#
    VB
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) 
      oleDbDataAdapter1.Fill(dataSet11) 
      ' Initialize a new RasterCodecs object 
      _codecs = New RasterCodecs() 
      _viewer = New ImageViewer() 
      _viewer.BackColor = Color.Bisque 
      _viewer.Dock = DockStyle.Fill 
      Controls.Add(_viewer) 
      _viewer.BringToFront() 
      _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 
       _codecs = new RasterCodecs(); 
                        
       _viewer = new ImageViewer(); 
       _viewer.BackColor = Color.Bisque; 
       _viewer.Dock = DockStyle.Fill; 
       Controls.Add(_viewer); 
       _viewer.BringToFront(); 
       _viewer.DataBindings.Add(new System.Windows.Forms.Binding("BindingData", 
                                                                 bindingSource1, 
                                                                 "Image", 
                                                                 true)); 
    } 
                     
         

  4. Add the following code to the Form1 class:

    C#
    VB
    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); 
      } 
    } 
                     
         

  5. Add the following code to the Form1 class:

    C#
    VB
    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); 
    } 
                     
         

  6. Build, and Run the program to test it.

  7. Save this project to use for testing other code samples.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize