silverlight實現圖片局部放大效果的方法。本站提示廣大學習愛好者:(silverlight實現圖片局部放大效果的方法)文章只能為提供參考,不一定能成為您想要的結果。以下是silverlight實現圖片局部放大效果的方法正文
本文實例講述了silverlight實現圖片局部放大效果的方法。分享給大家供大家參考,具體如下:
很多購物平台中(比如京東購物),浏覽產品詳情時都有這種效果,前幾天看到有朋友問SL能不能實現,當然可以
界面:
1.左側小圖片(用一個矩形Fill一張圖片即可)
2.左側半透明矩形
3.右側大圖片(用一個Canvas設置Clip裁剪可視區域作為蒙板,圖片放置在Canvas中即可)
原理:
獲取左側半透明矩形的相對位置,然後動態調整右側大圖的Canvas.Left與Canvas.Top
需要知道以下技術點:
1.Clip的應用
2.如何拖動對象
3.拖動時的邊界檢測
4.動態調整對象的Canvas.Left與Canvas.Top屬性
尺寸要點:
1.右側大圖可視區域與左側半透明矩形的“長寬比例”應該相同
2.“圖片原始尺寸長度比” 應該 “與左側小圖片長度比”相同
3.圖片原始大小/左側小圖大小 = 右側可視區域大小/半透明矩形大小
關鍵代碼:
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace PartMagnifier { public partial class MainPage : UserControl { bool trackingMouseMove = false; Point mousePosition; public MainPage() { // 為初始化變量所必需 InitializeComponent(); } private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e) { Adjust(); } private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; mousePosition = e.GetPosition(element); trackingMouseMove = true; if (null != element) { element.CaptureMouse(); element.Cursor = Cursors.Hand; } Adjust(); Debug(); sb.Begin();//標題動畫,可去掉 } private void Rectangle_MouseMove(object sender, MouseEventArgs e) { FrameworkElement element = sender as FrameworkElement; if (trackingMouseMove) { double deltaV = e.GetPosition(element).Y - mousePosition.Y; double deltaH = e.GetPosition(element).X - mousePosition.X; double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty); double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty); if (newLeft <= 10) { newLeft = 10; } if (newLeft >= 130) { newLeft = 130; } if (newTop <= 10) { newTop = 10; } if (newTop >= 85) { newTop = 85; } element.SetValue(Canvas.TopProperty, newTop); element.SetValue(Canvas.LeftProperty, newLeft); mousePosition = e.GetPosition(element); Adjust(); if (mousePosition.X <= 0 || mousePosition.Y <= 0) { return; } Debug(); } } private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; trackingMouseMove = false; element.ReleaseMouseCapture(); mousePosition.X = mousePosition.Y = 0; element.Cursor = null; } /// <summary> /// 調試信息 /// </summary> void Debug() { txtResult.Text = "鼠標相對坐標:" + mousePosition.ToString() + "\n小框left:" + rect.GetValue(Canvas.LeftProperty) + ",小框top:" + rect.GetValue(Canvas.TopProperty) + "\n大圖left:" + ((double)img.GetValue(Canvas.LeftProperty)).ToString("F0") + ",大圖right:" + ((double)img.GetValue(Canvas.TopProperty)).ToString("F0"); } /// <summary> /// 調整右側大圖的位置 /// </summary> void Adjust() { double n = cBig.Width / rect.Width; double left = (double)rect.GetValue(Canvas.LeftProperty) - 10; double top = (double)rect.GetValue(Canvas.TopProperty) - 10; double newLeft = -left * n; double newTop = -top * n; img.SetValue(Canvas.LeftProperty, newLeft); img.SetValue(Canvas.TopProperty, newTop); } } }
更多關於C#相關內容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
[db:作者簡介][db:原文翻譯及解析]