WPF/Silverlight實(shí)現(xiàn)圖片局部放大的方法分析
本文實(shí)例講述了WPF/Silverlight實(shí)現(xiàn)圖片局部放大的方法。分享給大家供大家參考,具體如下:
最近的項(xiàng)目中也要用到一個(gè)局部圖片放大的功能,前面這篇《silverlight實(shí)現(xiàn)圖片局部放大效果的方法》雖然已經(jīng)給出了原理、知識(shí)要點(diǎn)、尺寸要點(diǎn)及后端主要代碼,但遺憾的是沒有給出xaml的代碼。這里按照前文中的提示,動(dòng)手用WPF實(shí)踐了一下,花了一個(gè)小時(shí),終于搞出來了。這篇文章也就算是一個(gè)補(bǔ)充吧。
界面如下圖所示:
實(shí)現(xiàn)的原理和用到的知識(shí)點(diǎn)請(qǐng)點(diǎn)擊上面的鏈接,楊大俠已經(jīng)說的很清楚了。這里主要強(qiáng)調(diào)的就是尺寸要點(diǎn):
右側(cè)大圖可視區(qū)域與左側(cè)半透明矩形的“長(zhǎng)寬比例”應(yīng)該相同
“圖片原始尺寸長(zhǎng)度比” 應(yīng)該 “與左側(cè)小圖片長(zhǎng)度比”相同
圖片原始大小/左側(cè)小圖大小 = 右側(cè)可視區(qū)域大小/半透明矩形大小
為了簡(jiǎn)單起見,我們把尺寸固定死(其實(shí)是可以搞成活的),這里僅為了演示,以下尺寸滿足上面的條件。
準(zhǔn)備一張?jiān)瓐D:dog.jpg 分辨率:1920 * 1080
左側(cè)小圖片顯示區(qū)域:用Canvas 顯示,尺寸:320 * 180
半透明矩形框尺寸:50*50
右側(cè)大圖顯示區(qū)域:用Canvas顯示,尺寸:300 * 300
以下是XAML代碼:
<Window x:Class="WpfZoom.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF局部放大效果" Height="370" Width="700"> <Canvas x:Name="RootCanvas"> <!--左側(cè)小圖--> <Canvas x:Name="SmallBox" Width="320" Height="180" Canvas.Left="20" Canvas.Top="20"> <Canvas.Background> <ImageBrush ImageSource="Images/dog.jpg" Stretch="UniformToFill" /> </Canvas.Background> <!--半透明矩形框--> <Rectangle x:Name="MoveRect" Fill="White" Opacity="0.3" Stroke="Red" Width="50" Height="50" Canvas.Top="78" Canvas.Left="202" MouseMove="MoveRect_MouseMove" MouseLeftButtonDown="MoveRect_MouseLeftButtonDown" MouseLeftButtonUp="MoveRect_MouseLeftButtonUp"/> </Canvas> <!--右側(cè)大圖--> <Canvas x:Name="BigBox" Width="300" Height="300" Canvas.Left="360" Canvas.Top="20"> <!--右側(cè)原圖片 注意尺寸--> <Image x:Name="bigImg" Width="1920" Height="1080" Canvas.Left="0" Canvas.Top="-780" Source="Images/dog.jpg" /> <Canvas.Clip> <RectangleGeometry Rect="0,0,300,300" /> </Canvas.Clip> </Canvas> </Canvas> </Window>
cs 代碼:
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfZoom { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { //移動(dòng)標(biāo)志 bool trackingMouseMove = false; //鼠標(biāo)按下去的位置 Point mousePosition; public MainWindow() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { AdjustBigImage(); } /// <summary> /// 半透明矩形框鼠標(biāo)左鍵按下 /// </summary> private void MoveRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; mousePosition = e.GetPosition(element); trackingMouseMove = true; if (null != element) { //強(qiáng)制獲取此元素 element.CaptureMouse(); element.Cursor = Cursors.Hand; } } /// <summary> /// 半透明矩形框鼠標(biāo)左鍵彈起 /// </summary> private void MoveRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { FrameworkElement element = sender as FrameworkElement; trackingMouseMove = false; element.ReleaseMouseCapture(); mousePosition.X = mousePosition.Y = 0; element.Cursor = null; } /// <summary> /// 半透明矩形框移動(dòng) /// </summary> private void MoveRect_MouseMove(object sender, MouseEventArgs e) { FrameworkElement element = sender as FrameworkElement; if (trackingMouseMove) { //計(jì)算鼠標(biāo)在X軸的移動(dòng)距離 double deltaV = e.GetPosition(element).Y - mousePosition.Y; //計(jì)算鼠標(biāo)在Y軸的移動(dòng)距離 double deltaH = e.GetPosition(element).X - mousePosition.X; //得到圖片Top新位置 double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty); //得到圖片Left新位置 double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty); //邊界的判斷 if (newLeft <= 0) { newLeft = 0; } //左側(cè)圖片框?qū)挾?- 半透明矩形框?qū)挾? if (newLeft >= (this.SmallBox.Width - this.MoveRect.Width)) { newLeft = this.SmallBox.Width - this.MoveRect.Width; } if (newTop <= 0) { newTop = 0; } //左側(cè)圖片框高度度 - 半透明矩形框高度度 if (newTop >= this.SmallBox.Height - this.MoveRect.Height) { newTop = this.SmallBox.Height - this.MoveRect.Height; } element.SetValue(Canvas.TopProperty, newTop); element.SetValue(Canvas.LeftProperty, newLeft); AdjustBigImage(); if (mousePosition.X <= 0 || mousePosition.Y <= 0) { return; } } } /// <summary> /// 調(diào)整右側(cè)大圖的位置 /// </summary> void AdjustBigImage() { //獲取右側(cè)大圖框與透明矩形框的尺寸比率 double n = this.BigBox.Width / this.MoveRect.Width; //獲取半透明矩形框在左側(cè)小圖中的位置 double left = (double)this.MoveRect.GetValue(Canvas.LeftProperty); double top = (double)this.MoveRect.GetValue(Canvas.TopProperty); //計(jì)算和設(shè)置原圖在右側(cè)大圖框中的Canvas.Left 和 Canvas.Top double newLeft = -left * n; double newTop = -top * n; bigImg.SetValue(Canvas.LeftProperty, newLeft); bigImg.SetValue(Canvas.TopProperty, newTop); } } }
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
PS:這里再為大家推薦幾款比較實(shí)用的圖片處理工具供大家參考使用:
在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_img
在線Email郵箱圖標(biāo)制作工具:
http://tools.jb51.net/email/emaillogo
在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
更多相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:詳談C++引用&和指針在作為形參時(shí)的區(qū)別
欄 目:C#教程
下一篇:淺談static a[n*m]={0};中static的作用
本文標(biāo)題:WPF/Silverlight實(shí)現(xiàn)圖片局部放大的方法分析
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5836.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載