国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

WPF/Silverlight實現(xiàn)圖片局部放大的方法分析

2020-01-24 00:43:43
字體:
供稿:網(wǎng)友

本文實例講述了WPF/Silverlight實現(xiàn)圖片局部放大的方法。分享給大家供大家參考,具體如下:

最近的項目中也要用到一個局部圖片放大的功能,前面這篇《silverlight實現(xiàn)圖片局部放大效果的方法》雖然已經(jīng)給出了原理、知識要點、尺寸要點及后端主要代碼,但遺憾的是沒有給出xaml的代碼。這里按照前文中的提示,動手用WPF實踐了一下,花了一個小時,終于搞出來了。這篇文章也就算是一個補充吧。

界面如下圖所示:

實現(xiàn)的原理和用到的知識點請點擊上面的鏈接,楊大俠已經(jīng)說的很清楚了。這里主要強調(diào)的就是尺寸要點:

右側(cè)大圖可視區(qū)域與左側(cè)半透明矩形的“長寬比例”應(yīng)該相同
“圖片原始尺寸長度比” 應(yīng)該 “與左側(cè)小圖片長度比”相同
圖片原始大小/左側(cè)小圖大小 = 右側(cè)可視區(qū)域大小/半透明矩形大小

為了簡單起見,我們把尺寸固定死(其實是可以搞成活的),這里僅為了演示,以下尺寸滿足上面的條件。

準(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)      {        //強制獲取此元素        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);    }  }}

附:完整實例代碼點擊此處本站下載

PS:這里再為大家推薦幾款比較實用的圖片處理工具供大家參考使用:

在線圖片轉(zhuǎn)換BASE64工具:
http://tools.VeVB.COm/transcoding/img2base64

ICO圖標(biāo)在線生成工具:
http://tools.VeVB.COm/aideddesign/ico_img

在線Email郵箱圖標(biāo)制作工具:
http://tools.VeVB.COm/email/emaillogo

在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.VeVB.COm/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è)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 余干县| 平江县| 民勤县| 浦北县| 安平县| 东方市| 轮台县| 雅安市| 梅河口市| 榆林市| 丹寨县| 土默特左旗| 高邑县| 墨江| 平陆县| 正阳县| 慈溪市| 梧州市| 法库县| 水富县| 沽源县| 乌审旗| 张家港市| 开阳县| 甘谷县| 五常市| 本溪市| 通山县| 湟源县| 贵州省| 清流县| 称多县| 盐亭县| 徐州市| 小金县| 彭阳县| 津南区| 和林格尔县| 波密县| 林周县| 乳山市|