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 2012
  2. Choose File->New->Project... from the menu.
  3. In the New Project dialog box, choose Visual C# and Windows Store as the project template, and choose Blank App (XAML) in the Templates List.
  4. Type the project name as Updating a Gauge 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 for the project and select Add Reference... from the context menu. In the Reference Manager dialog box, select Leadtools.Controls from the Windows=>Extension list.

    Browse to the <LEADTOOLS_INSTALLDIR>\Bin\WinRT8_1\ folder (depending on your target platform), and select the following .WINMD files:

    • Leadtools.winmd
    • Leadtools.Kernel.winmd
    • Leadtools.Codecs.winmd
    • Leadtools.Codecs.Kernel.winmd
    • Leadtools.Codecs.Bmp.winmd
    • Leadtools.Codecs.Cmp.winmd
    • Leadtools.Codecs.Fax.winmd
    • Leadtools.Codecs.Gif.winmd
    • Leadtools.Codecs.J2k.winmd
    • Leadtools.Codecs.Png.winmd
    • Leadtools.Codecs.Tif.winmd
    • Leadtools.ImageProcessing.Utilities.winmd
    • Leadtools.ImageProcessing.Core.winmd
    • Leadtools.Dicom.winmd
    • Leadtools.Converters.winmd

    Click the Select button and then press the OK button to add the above references to the application.

  6. Open the MainPage.xaml file and copy the below XAML code into the editor:

    XAML
    <Page 
       x:Class="Updating_a_Guage.MainPage" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="using:Updating_a_Guage" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:custom="using:Leadtools.Controls" 
       mc:Ignorable="d"> 
                                          
       <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
          <StackPanel> 
             <Button x:Name="_loadButton" Click="_loadButton_Click">Load</Button> 
             <Button x:Name="_cmdButton" Click="_cmdButton_Click">Command</Button> 
             <ProgressBar x:Name="_progress" Minimum="0" Maximum="100"></ProgressBar> 
             <custom:RasterImageViewer x:Name="_viewer"  
                                            HorizontalAlignment="Stretch"  
                                            VerticalAlignment="Stretch"  
                                            ImageHorizontalAlignment="Center"  
                                            ImageVerticalAlignment="Center"  
                                            SizeMode="Fit"  
                                            NewImageResetOptions="All"/> 
          </StackPanel> 
       </Grid> 
    </Page> 

  7. Double-click on the Package.appxmanifest file. On the Declarations tab, add the File Open Picker as a Supported Declaration. Also, check the Supports any file type checkbox.

  8. Switch to MainPage.xaml code view (right-click Page.xaml in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    C#
    using Windows.Storage; 
    using Windows.Storage.Pickers; 
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Converters; 
    using Leadtools.ImageProcessing; 
    using Leadtools.ImageProcessing.Core; 

  9. Add the following class function:

    C#
    private async void _loadButton_Click(object sender, RoutedEventArgs e) 
    { 
       FileOpenPicker openPicker = new FileOpenPicker(); 
       openPicker.ViewMode = PickerViewMode.Thumbnail; 
       openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
       openPicker.FileTypeFilter.Add(".cmp"); 
       openPicker.FileTypeFilter.Add(".jpg"); 
       openPicker.FileTypeFilter.Add(".tif"); 
                        
       StorageFile file = await openPicker.PickSingleFileAsync(); 
       if (null != file) 
       { 
          try 
          { 
             //Open the file as a WinRT RandomAccessStream and create an ILeadStream from this RandomAccessStream 
             ILeadStream leadStream = LeadStreamFactory.Create(file); 
             using (IDisposable disposable = leadStream as IDisposable) 
             { 
                using (RasterCodecs codecs = new RasterCodecs()) 
                { 
                   _viewer.Image = await codecs.LoadAsync(leadStream); 
                } 
             } 
          } 
          catch (Exception exception) 
          { 
             string message = exception.Message; 
             RasterException rasterException = RasterException.FromHResult(exception.HResult); 
             if (rasterException != null) 
                message = rasterException.Message; 
                System.Diagnostics.Debug.WriteLine(message); 
          } 
       } 
    } 

  10. Add the following code to the MainPage class:

    C#
    private RasterImage _image; 
    private bool _cancel; 
    private void _cancelButton_Click(object sender, RoutedEventArgs e) 
    { 
       _cancel = true; 
    } 
                          
    private async void _cmdButton_Click(object sender, RoutedEventArgs e) 
    { 
       _image = _viewer.Image.Clone(); 
       await Task.Run(() => 
       { 
          MedianCommand cmd = new MedianCommand(40); 
          cmd.Progress += cmd_Progress; 
          _cancel = false; 
          try 
          { 
             cmd.Run(_image); 
          } 
          catch (Exception err) 
          { 
             string message = err.Message; 
             RasterException rasterException = RasterException.FromHResult(err.HResult); 
                if (rasterException != null) 
                message = rasterException.Message; 
                System.Diagnostics.Debug.WriteLine(message); 
           } 
           finally 
           { 
              cmd.Progress -= cmd_Progress; 
           } 
       }); 
       if(_cancel != true) 
          await _viewer.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
          () => 
          { 
             _viewer.Image = _image; 
          }); 
    } 
                          
    async void cmd_Progress(object sender, RasterCommandProgressEventArgs e) 
    { 
       await _progress.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
       () => 
       { 
          e.Cancel = _cancel; 
          _progress.Value = e.Percent;//update progress bar 
       }); 
    } 

  11. Build, and Run the program to test it. NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize