WPF/Silverlight實(shí)現(xiàn)圖片局部放大的方法分析
本文實(shí)例講述了WPF/Silverlight實(shí)現(xiàn)圖片局部放大的方法。分享給大家供大家參考,具體如下:
最近的項(xiàng)目中也要用到一個局部圖片放大的功能,前面這篇《silverlight實(shí)現(xiàn)圖片局部放大效果的方法》雖然已經(jīng)給出了原理、知識要點(diǎn)、尺寸要點(diǎn)及后端主要代碼,但遺憾的是沒有給出xaml的代碼。這里按照前文中的提示,動手用WPF實(shí)踐了一下,花了一個小時,終于搞出來了。這篇文章也就算是一個補(bǔ)充吧。
界面如下圖所示:

實(shí)現(xiàn)的原理和用到的知識點(diǎn)請點(diǎn)擊上面的鏈接,楊大俠已經(jīng)說的很清楚了。這里主要強(qiáng)調(diào)的就是尺寸要點(diǎn):
右側(cè)大圖可視區(qū)域與左側(cè)半透明矩形的“長寬比例”應(yīng)該相同
“圖片原始尺寸長度比” 應(yīng)該 “與左側(cè)小圖片長度比”相同
圖片原始大小/左側(cè)小圖大小 = 右側(cè)可視區(qū)域大小/半透明矩形大小
為了簡單起見,我們把尺寸固定死(其實(shí)是可以搞成活的),這里僅為了演示,以下尺寸滿足上面的條件。
準(zhǔn)備一張原圖: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
{
//移動標(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>
/// 半透明矩形框移動
/// </summary>
private void MoveRect_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
//計算鼠標(biāo)在X軸的移動距離
double deltaV = e.GetPosition(element).Y - mousePosition.Y;
//計算鼠標(biāo)在Y軸的移動距離
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);
//計算和設(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è)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
Unity創(chuàng)建平鋪網(wǎng)格地圖的方法
這篇文章主要為大家詳細(xì)介紹了Unity創(chuàng)建平鋪網(wǎng)格地圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
C#中TreeView節(jié)點(diǎn)的自定義繪制方法
這篇文章主要介紹了C#中TreeView節(jié)點(diǎn)的自定義繪制方法,實(shí)例展示了TreeView節(jié)點(diǎn)的操作技巧,需要的朋友可以參考下2015-02-02
實(shí)例分享C#中Explicit和Implicit用法
本篇文章主要給讀者們分享了C#中Explicit和Implicit的用法,對此有需求和興趣的朋友們一起學(xué)習(xí)下吧。2017-12-12
C#?網(wǎng)域賬號(Domain)驗(yàn)證的實(shí)現(xiàn)
本文主要介紹了C#?網(wǎng)域賬號(Domain)驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
C# 字符串string和內(nèi)存流MemoryStream及比特數(shù)組byte[]之間相互轉(zhuǎn)換
本文主要介紹字符串string和內(nèi)存流MemoryStream及比特數(shù)組byte[]之間相互轉(zhuǎn)換的方法,需要的小伙伴可以參考一下。2016-05-05
C#?設(shè)置Chart的X軸為時間軸???????詳情
這篇文章主要介紹了C#設(shè)置Chart的X軸為時間軸???????詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08

