The MagnifyGlassEffect Class supports WPF/Silverlight.
Creates an effect that can be used to zoom in on a portion of an image with options to specify size, shape and color of the magnify glass and crosshairs.
Supported in Silverlight
Object Model
Syntax
Example
Visual Basic | Copy Code |
---|
Class MagnifyGlassEffectExampleWindow
Inherits Window
Private theViewer As ImageViewer
Public Sub New()
Dim sp As New StackPanel()
Content = sp
theViewer = New ImageViewer()
theViewer.HorizontalAlignment = HorizontalAlignment.Center
theViewer.VerticalAlignment = VerticalAlignment.Top
theViewer.ImageHorizontalAlignment = HorizontalAlignment.Left
theViewer.ImageVerticalAlignment = VerticalAlignment.Top
sp.Children.Add(theViewer)
' Load an image into the viewer
theViewer.Source = New BitmapImage(New Uri(System.IO.Path.Combine(LEAD_VARS.ImagesDir, "Cannon.jpg")))
Title = "MagnifyGlassEffect - Click and and move the mouse cursor on the image to see the effect"
AddHandler theViewer.PreviewMouseDown, AddressOf theViewer_PreviewMouseDown
AddHandler theViewer.MouseMove, AddressOf theViewer_MouseMove
AddHandler theViewer.MouseUp, AddressOf theViewer_MouseUp
End Sub
Private Sub theViewer_PreviewMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim effect As New MagnifyGlassEffect()
Dim pos As Point = e.GetPosition(theViewer)
Dim source As BitmapSource = DirectCast(theViewer.Source, BitmapSource)
If Not source Is Nothing Then
effect.Center = New Point( _
(1.0 * pos.X) / source.PixelWidth, _
(1.0 * pos.Y) / source.PixelHeight)
effect.Radius = 0.25
effect.AspectRatio = 1.0
effect.ScaleFactor = 2.0
effect.Color = Colors.Blue
effect.Border = 1.0
effect.CrossHairs = 0.0
theViewer.ImageEffect = effect
End If
End Sub
Private Sub theViewer_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim effect As MagnifyGlassEffect = DirectCast(theViewer.ImageEffect, MagnifyGlassEffect)
If Not effect Is Nothing Then
Dim pos As Point = e.GetPosition(theViewer)
Dim source As BitmapSource = DirectCast(theViewer.Source, BitmapSource)
If Not source Is Nothing Then
effect.Center = New Point( _
(1.0 * pos.X) / source.PixelWidth, _
(1.0 * pos.Y) / source.PixelHeight)
End If
End If
End Sub
Private Sub theViewer_MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
If Not theViewer.ImageEffect Is Nothing Then
theViewer.ImageEffect = Nothing
End If
End Sub
End Class
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class |
C# | Copy Code |
---|
class MagnifyGlassEffectExampleWindow : Window
{
private ImageViewer theViewer;
public MagnifyGlassEffectExampleWindow()
{
StackPanel sp = new StackPanel();
Content = sp;
theViewer = new ImageViewer();
theViewer.HorizontalAlignment = HorizontalAlignment.Center;
theViewer.VerticalAlignment = VerticalAlignment.Top;
theViewer.ImageHorizontalAlignment = HorizontalAlignment.Left;
theViewer.ImageVerticalAlignment = VerticalAlignment.Top;
sp.Children.Add(theViewer);
// Load an image into the viewer
theViewer.Source = new BitmapImage(new Uri(System.IO.Path.Combine(LEAD_VARS.ImagesDir, "Cannon.jpg")));
Title = "MagnifyGlassEffect - Click and and move the mouse cursor on the image to see the effect";
theViewer.PreviewMouseDown += new MouseButtonEventHandler(theViewer_PreviewMouseDown);
theViewer.MouseMove += new MouseEventHandler(theViewer_MouseMove);
theViewer.MouseUp += new MouseButtonEventHandler(theViewer_MouseUp);
}
private void theViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MagnifyGlassEffect effect = new MagnifyGlassEffect();
Point pos = e.GetPosition(theViewer);
BitmapSource source = theViewer.Source as BitmapSource;
if(source != null)
{
effect.Center = new Point(
(1.0 * pos.X) / source.PixelWidth,
(1.0 * pos.Y) / source.PixelHeight);
effect.Radius = 0.25;
effect.AspectRatio = 1.0;
effect.ScaleFactor = 2.0;
effect.Color = Colors.Blue;
effect.Border = 1.0;
effect.CrossHairs = 0.0;
theViewer.ImageEffect = effect;
}
}
private void theViewer_MouseMove(object sender, MouseEventArgs e)
{
MagnifyGlassEffect effect = theViewer.ImageEffect as MagnifyGlassEffect;
if(effect != null)
{
Point pos = e.GetPosition(theViewer);
BitmapSource source = theViewer.Source as BitmapSource;
if(source != null)
{
effect.Center = new Point(
(1.0 * pos.X) / source.PixelWidth,
(1.0 * pos.Y) / source.PixelHeight);
}
}
}
private void theViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
if(theViewer.ImageEffect != null)
{
theViewer.ImageEffect = null;
}
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
} |
SilverlightCSharp | Copy Code |
---|
class MagnifyGlassEffectExampleWindow : UserControl
{
private ImageViewer theViewer;
public MagnifyGlassEffectExampleWindow()
{
StackPanel sp = new StackPanel();
Content = sp;
theViewer = new ImageViewer();
theViewer.HorizontalAlignment = HorizontalAlignment.Center;
theViewer.VerticalAlignment = VerticalAlignment.Top;
theViewer.ImageHorizontalAlignment = HorizontalAlignment.Left;
theViewer.ImageVerticalAlignment = VerticalAlignment.Top;
sp.Children.Add(theViewer);
// Load an image into the viewer
theViewer.Source = new BitmapImage(new Uri(LeadtoolsExamples.Common.ImagesPath.Path + "Cannon.jpg"));
theViewer.MouseMove += new MouseEventHandler(theViewer_MouseMove);
}
private void theViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MagnifyGlassEffect effect = new MagnifyGlassEffect();
Point pos = e.GetPosition(theViewer);
BitmapSource source = theViewer.Source as BitmapSource;
if(source != null)
{
effect.Center = new Point(
(1.0 * pos.X) / source.PixelWidth,
(1.0 * pos.Y) / source.PixelHeight);
effect.Radius = 0.25;
effect.AspectRatio = 1.0;
effect.ScaleFactor = 2.0;
effect.Color = Colors.Blue;
effect.Border = 1.0;
effect.CrossHairs = 0.0;
theViewer.ImageEffect = effect;
}
}
private void theViewer_MouseMove(object sender, MouseEventArgs e)
{
MagnifyGlassEffect effect = theViewer.ImageEffect as MagnifyGlassEffect;
if(effect != null)
{
Point pos = e.GetPosition(theViewer);
BitmapSource source = theViewer.Source as BitmapSource;
if(source != null)
{
effect.Center = new Point(
(1.0 * pos.X) / source.PixelWidth,
(1.0 * pos.Y) / source.PixelHeight);
}
}
}
private void theViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
if(theViewer.ImageEffect != null)
{
theViewer.ImageEffect = null;
}
}
} |
SilverlightVB | Copy Code |
---|
Class MagnifyGlassEffectExampleWindow
Inherits UserControl
Private theViewer As ImageViewer
Public Sub New()
Dim sp As New StackPanel()
Content = sp
theViewer = New ImageViewer()
theViewer.HorizontalAlignment = HorizontalAlignment.Center
theViewer.VerticalAlignment = VerticalAlignment.Top
theViewer.ImageHorizontalAlignment = HorizontalAlignment.Left
theViewer.ImageVerticalAlignment = VerticalAlignment.Top
sp.Children.Add(theViewer)
' Load an image into the viewer
theViewer.Source = New BitmapImage(New Uri(LeadtoolsExamples.Common.ImagesPath.Path + "Cannon.jpg"))
AddHandler theViewer.MouseMove, AddressOf theViewer_MouseMove
End Sub
Private Sub theViewer_PreviewMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim effect As New MagnifyGlassEffect()
Dim pos As Point = e.GetPosition(theViewer)
Dim source As BitmapSource = DirectCast(theViewer.Source, BitmapSource)
If Not source Is Nothing Then
effect.Center = New Point( _
(1.0 * pos.X) / source.PixelWidth, _
(1.0 * pos.Y) / source.PixelHeight)
effect.Radius = 0.25
effect.AspectRatio = 1.0
effect.ScaleFactor = 2.0
effect.Color = Colors.Blue
effect.Border = 1.0
effect.CrossHairs = 0.0
theViewer.ImageEffect = effect
End If
End Sub
Private Sub theViewer_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim effect As MagnifyGlassEffect = DirectCast(theViewer.ImageEffect, MagnifyGlassEffect)
If Not effect Is Nothing Then
Dim pos As Point = e.GetPosition(theViewer)
Dim source As BitmapSource = DirectCast(theViewer.Source, BitmapSource)
If Not source Is Nothing Then
effect.Center = New Point( _
(1.0 * pos.X) / source.PixelWidth, _
(1.0 * pos.Y) / source.PixelHeight)
End If
End If
End Sub
Private Sub theViewer_MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
If Not theViewer.ImageEffect Is Nothing Then
theViewer.ImageEffect = Nothing
End If
End Sub
End Class |
XAML | Copy Code |
---|
<Window x:Class="MagnifyGlassEffectExample" Height="600" Width="800" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:leadControls="clr-namespace:Leadtools.Windows.Controls;assembly=Leadtools.Windows.Controls" xmlns:leadEffects="clr-namespace:Leadtools.Windows.Media.Effects;assembly=Leadtools.Windows.Media.Effects">
<StackPanel>
<leadControls:ImageViewer HorizontalAlignment="Center" VerticalAlignment="Top" ImageHorizontalAlignment="Left" ImageVerticalAlignment="Top" Source="file:///c:\users\Public\Documents\LEADTOOLS Images\cannon.jpg">
<leadControls:ImageViewer.ImageEffect>
<leadEffects:MagnifyGlassEffect Center="0.5,0.5" Radius="0.25" AspectRatio="1.0" ScaleFactor="2.0" Color="Blue" Border="1.0" CrossHairs="1.0"></leadEffects:MagnifyGlassEffect>
</leadControls:ImageViewer.ImageEffect>
</leadControls:ImageViewer>
</StackPanel>
</Window> |
Inheritance Hierarchy
Requirements
Target Platforms: Silverlight 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only)
See Also