服务器之家

服务器之家 > 正文

silverlight实现图片局部放大效果的方法

时间:2021-12-29 13:25     来源/作者:菩提树下的杨过

本文实例讲述了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.图片原始大小/左侧小图大小 = 右侧可视区域大小/半透明矩形大小

关键代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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#程序设计有所帮助。

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部