Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Using a Pan Window and Interactive Modes in Silverlight

Take the following steps to start a project and to add some code that will demonstrate the various interactive modes of the LEADTOOLS Silverlight Control and the Pan Viewer:

  1. Start Visual Studio .NET.
  2. Start with the project that you created in Loading and displaying an image in Silverlight.
  3. Open the Page.xaml file and copy the below xaml code into the editor:

    [XAML]

    
    <UserControl x:Class="Load_And_Display.Page"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:Leadtools_Silverlight_Controls="clr-namespace:Leadtools.Silverlight.Controls;assembly=Leadtools.Silverlight.Controls"
      Width="800" Height="600">
      <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>       
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        
        <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" Grid.Column="0">
            <StackPanel Orientation="Vertical" >
                <Button Name="myButton" Content="Load" FontSize="30" Click="Button_Click" Height="40"></Button>
                <Leadtools_Silverlight_Controls:ImageViewer x:Name="viewerControl" SizeMode="Normal" Height="550"></Leadtools_Silverlight_Controls:ImageViewer> 
            </StackPanel>
        </ScrollViewer>
        
        <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" Grid.Column="1">
            <StackPanel Orientation="Vertical" >
                <TextBlock Text="Pan Window :"/>
                <Leadtools_Silverlight_Controls:ImagePanViewer x:Name="_imagePanViewer" Width="150" Height="150" HorizontalAlignment="Left"></Leadtools_Silverlight_Controls:ImagePanViewer>
                <TextBlock Text="Interactive Mode :"/>
                <ComboBox Name="_cmbInteractiveMode" SelectedIndex="0" Width="200" HorizontalAlignment="Left" SelectionChanged="ComboBox_SelectionChanged">
                    <ComboBoxItem Content="None"/>
                    <ComboBoxItem Content="Pan"/>
                    <ComboBoxItem Content="Center At"/>
                    <ComboBoxItem Content="Zoom To"/>
                    <ComboBoxItem Content="Scale"/>
                    <ComboBoxItem Content="Magnify Glass"/>
                </ComboBox>
            </StackPanel>
        </ScrollViewer>    
      </Grid>
    </UserControl>
    
  4. Switch to Page.xaml code view (right-click Page.xaml in the solution explorer then select View Code) and add the following class function:

    [Visual Basic]

    
    Private Sub ComboBox_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
      If _cmbInteractiveMode Is Nothing OrElse _cmbInteractiveMode.SelectedItem Is Nothing Then
         Return
      End If
    
      Dim item As String = (CType(IIf(TypeOf _cmbInteractiveMode.SelectedItem Is ComboBoxItem, _cmbInteractiveMode.SelectedItem, Nothing), ComboBoxItem)).Content.ToString()
    
      Select Case item
         Case "Pan"
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.Pan
    
         Case "Center At"
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.CenterAt
    
         Case "Magnify Glass"
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.MagnifyGlass
    
         Case "Scale"
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.Scale
    
         Case "Zoom To"
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.ZoomTo
    
         Case Else
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.None
      End Select
    End Sub
    

    [C#]

    
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (_cmbInteractiveMode == null || _cmbInteractiveMode.SelectedItem == null)
         return;
    
      string item = (_cmbInteractiveMode.SelectedItem as ComboBoxItem).Content.ToString();
    
      switch (item)
      {
         case "Pan":
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.Pan;
            break;
    
         case "Center At":
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.CenterAt;
            break;
    
         case "Magnify Glass":
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.MagnifyGlass;
            break;
    
         case "Scale":
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.Scale;
            break;
    
         case "Zoom To":
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.ZoomTo;
            break;
    
         default:
            viewerControl.InteractiveMode = Leadtools.Silverlight.Controls.ImageViewerInteractiveMode.None;
            break;
      }
    }
    

  5. Update the Button_Click event as shown below:

    [Visual Basic]

    
    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
      Try  
         Dim ofd As OpenFileDialog = New OpenFileDialog()
         If ofd.ShowDialog() = True Then
            Dim fileStream As FileStream = ofd.File.OpenRead()
            Dim bi As BitmapImage = New BitmapImage()
            bi.SetSource(fileStream)
            viewerControl.Source = bi
            _imagePanViewer.Source = viewerControl
            fileStream.Close()
         End If
      Catch ex As Exception
         MessageBox.Show(ex.Message)
      End Try
    End Sub
    

    [C#]

    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
      try
      {
         OpenFileDialog ofd = new OpenFileDialog();
         if (ofd.ShowDialog() == true)
         {
            FileStream fileStream = ofd.File.OpenRead();
            BitmapImage bi = new BitmapImage();
            bi.SetSource(fileStream);
            viewerControl.Source = bi;
            _imagePanViewer.Source = viewerControl;
            fileStream.Close();
         }
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
    }
    

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

    Click the "Load" button, and select a valid JPEG or PNG image.