Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 3 January 2009

Silverlight 2 : Using Magnifier to Zoom contents on Canvas

Posted on 10:46 by Unknown

First of all wish all my blog readers very Happy New Year ! , Thanks for your continuous feedback and support all the time. Some people given me feedback in 2008 that I wrote some article too basic and not to the level which they expect, frankly speaking , I wrote here what comes to my mind, what I see and what I feel difficult to understand to others, so I always try my best to keep things simple and quick.But I will do follow your feedback so be with me in year 2009 since lots new on this blog going to be there in coming few months !

There are several post on usage of Magnifier in Silverlight 1.1 as well as in 2.0, Some have done with Images and some with Videos. I am re-making this code snippet which is already published by Jeff Prosise’s blog and code snippet which I am going to share here is already available on one of the PDC 2008 snippet at : http://www.wintellect.com/Downloads/PDC_2008_Demos.zip

Idea of doing this again re-work and twick the already written source code is to create awareness about the Magnifier which can be use efficiently for zooming part of image or video.

Step 1 : Create a Layout which will hold 3 Canvas.

<Grid x:Name="LayoutRoot" Background="Black" MouseLeftButtonDown="OnMouseLeftButtonDown" MouseMove="OnMouseMove" MouseLeftButtonUp="OnMouseLeftButtonUp">

Note that, for Zooming particular portion of Canvas, we are suppose to handle mouse events on all over base canvas which you can see from above events.

<Canvas x:Name="CoreCanvas" Width="800" Height="530">
            <!-- Base canvas -->
            <Canvas x:Name="BaseCanvas" Canvas.Left="0" Canvas.Top="0" Width="800" Height="400" Background="Black">
                <Canvas Canvas.Left="90" Canvas.Top="30" Width="620" Height="470">
                    <Rectangle Canvas.Left="0" Canvas.Top="0" Width="620" Height="470" Fill="White" />
                    <MediaElement Canvas.Left="10" Canvas.Top="10" Width="600" Height="450" Source="Bear.wmv" />
                </Canvas>
            </Canvas>

Above is the Canvas which will hold Video / Image file on which we will put magnifier for zooming.

<!-- Magnify canvas -->
<Canvas x:Name="MagnifyCanvas" Canvas.Left="0" Canvas.Top="0" Width="800" Height="400" Background="Black" Visibility="Collapsed">
                <Canvas.RenderTransform>
                    <ScaleTransform CenterX="0" CenterY="0" ScaleX="4" ScaleY="4"/>
                </Canvas.RenderTransform>
                <Canvas.Clip>
                    <EllipseGeometry x:Name="Lens" Center="0,0" RadiusX="40" RadiusY="40" />
                </Canvas.Clip>
                <Canvas Canvas.Left="90" Canvas.Top="30" Width="620" Height="470">
                    <Rectangle Canvas.Left="0" Canvas.Top="0" Width="620" Height="470" Fill="White" />
                    <MediaElement Canvas.Left="10" Canvas.Top="10" Width="600" Height="450" Source="Bear.wmv" />
                </Canvas>
                <Path Canvas.Left="0" Canvas.Top="0" StrokeThickness="0">
                    <Path.Data>
                         <EllipseGeometry x:Name="LensBorder" Center="0,0" RadiusX="40" RadiusY="40" />
                    </Path.Data>
                </Path>
            </Canvas>
        </Canvas>
      </Grid>

Above code will add another Canvas with Ellipse which is our circular magnifier for zooming, you can try out with RectangleGeometry.

Step 2 : Create a general method as AdjustLens() for maitaining the Magnifier value and position.

Variables declare for drag and scale which keep tracks whether drag by mouse for Magnification or not.

private bool drag = false;
private const double scale = 4.0;

private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
            double x = e.GetPosition(CoreCanvas).X;
            double y = e.GetPosition(CoreCanvas).Y;
            AdjustLens(x, y);

            MagnifyCanvas.Visibility = Visibility.Visible;
            ((FrameworkElement)sender).CaptureMouse();
            drag = true;
}

OnMouseLeftButtonDown basically pass the position of CoreCanvas to AdjustLens() which keep tracks of all the movements on Canvas.Same for rest of the events.

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                double x = e.GetPosition(BaseCanvas).X;
                double y = e.GetPosition(BaseCanvas).Y;
                AdjustLens(x, y);
            }
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (drag)
            {
                MagnifyCanvas.Visibility = Visibility.Collapsed;
                ((FrameworkElement)sender).ReleaseMouseCapture();
                drag = false;
            }
        }

AdjustLens() helps to position Magnifier as per mouse movements and position of Canvas.

private void AdjustLens(double x, double y)
        {
            Lens.Center = LensBorder.Center = new Point(x, y);
            MagnifyCanvas.SetValue(Canvas.LeftProperty, (1 - scale) * x);
            MagnifyCanvas.SetValue(Canvas.TopProperty, (1 - scale) * y);
        }
  So build the project, you will get output as follows.

mag1

After applying Magnifier by dragging mouse, it will look like..

mag3

If you notice, you can see a Big Ellipse on right corner of the screen which shows a Bird is zoomed up, you can implement Magnifier while Video is in Run mode and it will not disturb the execution of video neither it will break any frame, It will run as smooth as it runs normally.

Hope this post will give you idea about implementation of Magnifier in Silverlight application and inspire you to implement it like I inspired and remake it from Jeff’s blog.

Vikram. 

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ▼  January (6)
      • Microsoft Certification - Discount Code for MCTS,M...
      • Lap Around Microsoft Web Platform Installer 1.0 ak...
      • Silverlight 2 : Maintain data in Session using Sil...
      • Microsoft Tag : New way to go Online !!
      • Lap around Report Builder 2.0
      • Silverlight 2 : Using Magnifier to Zoom contents o...
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile