yfp15062328347 2016-03-21 07:41 采纳率: 0%
浏览 2303

wpf实现图片拖动缩放,新增滚动缩放 到图片原本大小就不缩放,拖动图片到边角就不拖动

/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{

    public UserControl1()
    {
        InitializeComponent();

        WPFWindow.MouseWheel += MainWindow_MouseWheel;

        image.MouseLeftButtonDown += image_MouseLeftButtonDown;
        image.MouseLeftButtonUp += image_MouseLeftButtonUp;
        image.MouseMove += image_MouseMove;

    }
    private Point origin;
    private Point start;
    /// <summary>
    /// 图片路径
    /// </summary>
    /// <param name="fileName"></param>
    public void SetSource(string fileName)
    {
        image.Source = new BitmapImage(new Uri(fileName));
    }
    /// <summary>
    /// 程序高度和宽度
    /// </summary>
    /// <param name="height"></param>
    /// <param name="width"></param>
    public void setHeight_Width(int height,int width)
    {
        WPFWindow.Height = height;
        WPFWindow.Width = width;
    }


    private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        image.ReleaseMouseCapture();
    }

    private void image_MouseMove(object sender, MouseEventArgs e)
    {
        if (!image.IsMouseCaptured) return;
        Point p = e.MouseDevice.GetPosition(border);

        Matrix m = image.RenderTransform.Value;
        m.OffsetX = origin.X + (p.X - start.X);
        m.OffsetY = origin.Y + (p.Y - start.Y);

        image.RenderTransform = new MatrixTransform(m);
    }

    private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (image.IsMouseCaptured) return;
        image.CaptureMouse();

        start = e.GetPosition(border);
        origin.X = image.RenderTransform.Value.OffsetX;
        origin.Y = image.RenderTransform.Value.OffsetY;
    }

    private void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        Point p = e.MouseDevice.GetPosition(image);

        Matrix m = image.RenderTransform.Value;
        if (e.Delta > 0)
            m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
        else
            m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);

        image.RenderTransform = new MatrixTransform(m);
    }
            /*xaml 页面*/
            <UserControl x:Class="WpfControlLibrary1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
          x:Name="WPFWindow"
         d:DesignHeight="610" d:DesignWidth="674"
         >
<Grid>
    <Border Grid.Row="1" Name="border" ClipToBounds="True">
        <Image Name="image" Opacity="1"  />
    </Border>
</Grid>

/**/
本人网上当的代码, 现在想新增功能呢,就是图片滚动缩放 到图片原本大小就不缩放了,拖动到图片的边角就不拖动了,

  • 写回答

1条回答 默认 最新

  • 小编能手 2020-03-31 10:52
    关注

    这个 思路很好,那个平移拖到边角既不能平移的有想法吗

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。