Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Updating a Gauge and Detecting a User Interrupt
Take the following steps to start a project and to add some code that updates a gauge during processing and detects a user interrupt:
  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 "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as "Updating a Guage and Detecting User Interrupt" 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 .
  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 "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    • Leadtools.ImageProcessing.Core.dll
    Click the Select button and then press the OK button to add the above DLLs to the application.
  6. Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and Drag and drop an instance of RasterImageViewer to the form. If you do not have RasterImageViewer in your toolbox, select Tools->Add Remove Toolbox Items from the menu. Click Browse and then select Leadtools.WinForms.DLL from "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32" and then click Open and then click OK.
  7. From the toolbox (View->Toolbox), add a ProgressBar, a Button (Text = "Median") and and another Button (Text = "Cancel").
  8. 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]

    
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms
    Imports Leadtools.ImageProcessing
    Imports Leadtools.ImageProcessing.Core
    
    [C#]
     
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;
    using Leadtools.ImageProcessing;
    using Leadtools.ImageProcessing.Core;
    
  9. Add an event handler to the Form1 Load event and add the following code:

    [Visual Basic]

    
    Private canceled As Boolean
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ' load an image into our viewer
       RasterCodecs.Startup()
       Dim codecs As New RasterCodecs()
       rasterImageViewer1.Image = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image3.cmp")
       RasterCodecs.Shutdown()
       
       ' disable the cancel button for now
       Button2.Enabled = False
    End Sub
    
    [C#]
    
    private bool canceled;
    
    private void Form1_Load(object sender, System.EventArgs e)
    {
       // load an image into our viewer
       RasterCodecs.Startup();
       RasterCodecs codecs = new RasterCodecs();
       rasterImageViewer1.Image = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image3.cmp");
       RasterCodecs.Shutdown();
       
       // disable the cancel button for now
       button2.Enabled = false;
    }
    
  10. double-click the button1 Button (Median button) on the form to add the event handler for it and add the following code:

    [Visual Basic]

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       canceled = False
       ProgressBar1.Value = 0
       
       ' disable the median button and enable the cancel button
       Button1.Enabled = False
       Button2.Enabled = True
       
       Dim command As New MedianCommand(40)
       
       ' subscribe the progress event
       AddHandler command.Progress, AddressOf median_Progress
       
       ' Run median command
       command.Run(RasterImageViewer1.Image)
       
       ' enable the median button and disable the cancel button
       Button1.Enabled = True
       Button2.Enabled = False
       ProgressBar1.Value = 0
    End Sub
    
    Private Sub median_Progress(ByVal sender As System.Object, ByVal e As RasterCommandProgressEventArgs)
       ' update the value of the progress bar
       ProgressBar1.Value = e.Percent
       
       ' check wethear the cancel button has been pressed
       If (canceled) Then
          e.Cancel = True
       End If
       
       Application.DoEvents()
    End Sub
    
    [C#]
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       canceled = false;
       progressBar1.Value = 0;
       
       // disable the median button and enable the cancel button
       button1.Enabled = false;
       button2.Enabled = true;
       
       MedianCommand command = new MedianCommand(40);
       
       // subscribe the progress event
       command.Progress += new EventHandler<RasterCommandProgressEventArgs>(median_Progress);
       
       // Run median command
       command.Run(rasterImageViewer1.Image);
       
       // enable the median button and disable the cancel button
       button1.Enabled = true;
       button2.Enabled = false;
       progressBar1.Value = 0;
    }
       
    private void median_Progress(object sender, RasterCommandProgressEventArgs e)
    {
       // update the value of the progress bar
       progressBar1.Value = e.Percent;
       
       // check wethear the cancel button has been pressed
       if(canceled)
          e.Cancel = true;
       
       Application.DoEvents();
    }
    
  11. double-click the button2 Button (Cancel button) on the form to add the event handler for it and add the following code: [Visual Basic]
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       ' Cancel the median command
       canceled = True
    End Sub
    
    [C#]
    
    private void button2_Click(object sender, System.EventArgs e)
    {
       // Cancel the median command
       canceled = true;
    }
    
  12. Build, and Run the program to test it.