图片控件的修改

这次实现了拖动和拖动边界控制:

XMAL:

<UserControl x:Class="DesignerSL.Views.Gifts.MiniPhotoControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:adorners="clr-namespace:Adorners"
    mc:Ignorable="d"
    d:DesignHeight="301" d:DesignWidth="222" Loaded="UserControl_Loaded">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid Name="gidPanel" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="0" >
            <Canvas x:Name="panelDisplay">
                <Canvas.Clip>
                    <RectangleGeometry x:Name="RectangGeome" />
                </Canvas.Clip>
                <Image Name="imgPhoto" Height="auto" Width="auto" Canvas.ZIndex="0" MouseLeftButtonDown="imgPhoto_MouseLeftButtonDown" MouseLeftButtonUp="imgPhoto_MouseLeftButtonUp" MouseMove="imgPhoto_MouseMove">
                    <!--<Image.Clip>
                        <RectangleGeometry x:Name="RectangGeome" />
                    </Image.Clip>-->
                </Image>
            </Canvas>
            <!--<adorners:CursorPlane x:Name='cursorPlane' Canvas.Left="-38" Canvas.Top="-2" IsHitTestVisible="True" />-->
        </Grid>
    </Grid>

</UserControl>
代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Adorners;
using Adorners.Frames;

namespace DesignerSL.Views.Gifts
{
    public partial class MiniPhotoControl : UserControl
    {
        CropFrame frame2 = new CropFrame();
        public double ImageX;
        public double ImageY;
        public PhotoInfo MiniImage { get; set; }
        public double ControlHeight;
        public double ControlWidth;
        private double MengbanX;
        private double MengbanY;

        //bool trackingMouseMove = false;
        //double deltax;
        //double deltay;
        //Point _point;


        public MiniPhotoControl()
        {
            InitializeComponent();

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Height = ControlHeight;
            this.Width = ControlWidth;
            imgPhoto.RenderTransform = new TranslateTransform();
            RectangGeome.Rect = new Rect(0, 0, ControlWidth, ControlHeight);
            AddPhoto();
        }
        private void AddPhoto()
        {
            WriteableBitmap PhotoData = MiniImage.PhotoData;

            if (ControlHeight / PhotoData.PixelHeight < ControlWidth / PhotoData.PixelWidth)
            {
                imgPhoto.Width = ControlWidth;
                imgPhoto.Height = PhotoData.PixelHeight * (ControlWidth / PhotoData.PixelWidth);
                MengbanX = ImageX = 0;
                MengbanY = ImageY = -(imgPhoto.Height - ControlHeight) / 2;
                Canvas.SetLeft(imgPhoto, 0);
                Canvas.SetTop(imgPhoto, MengbanY);
                //RectangGeome.Rect = new Rect(0, (imgPhoto.Height - ControlHeight) / 2, ControlWidth, ControlHeight);
            }
            else
            {
                imgPhoto.Height = ControlHeight;
                imgPhoto.Width = PhotoData.PixelWidth * (ControlHeight / PhotoData.PixelHeight);
                MengbanX = ImageX = -(imgPhoto.Width - ControlWidth) / 2;
                MengbanY = ImageY = 0;
                Canvas.SetTop(imgPhoto, 0);
                Canvas.SetLeft(imgPhoto, MengbanX);
                //RectangGeome.Rect = new Rect((imgPhoto.Width - ControlWidth) / 2,0,  ControlWidth, ControlHeight);
            }
            imgPhoto.Source = PhotoData;

        }

        void imgPhoto_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

                        FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();
            mousePosition.X = mousePosition.Y = 0;
            element.Cursor = null;
            ImageX = (double)element.GetValue(Canvas.LeftProperty);
            ImageY = (double)element.GetValue(Canvas.TopProperty);
        }
        bool trackingMouseMove = false;
        Point mousePosition;
        void imgPhoto_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
                       FrameworkElement element = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            trackingMouseMove = true;
            if (null != element)
            {
                element.CaptureMouse();
                element.Cursor = Cursors.Hand;
            }

        }
        void imgPhoto_MouseMove(object sender, MouseEventArgs e)
        {
                       FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                double deltaH = e.GetPosition(null).X - mousePosition.X;
                double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);
                if (newTop < 0 & newTop > -System.Math.Abs(element.Height - ControlHeight))
                    element.SetValue(Canvas.TopProperty, newTop);
                else if (newTop > 0)
                {
                    element.SetValue(Canvas.TopProperty, 0.00);
                    //trackingMouseMove = false;
                }
                else if (newTop < -System.Math.Abs(element.Height - ControlHeight))
                {
                    element.SetValue(Canvas.TopProperty, -System.Math.Abs(element.Height - ControlHeight));
                    //trackingMouseMove = false;
                }
                if (newLeft < 0 & newLeft > -System.Math.Abs(element.Width - ControlWidth))
                    element.SetValue(Canvas.LeftProperty, newLeft);
                else if (newLeft > 0)
                {
                    element.SetValue(Canvas.LeftProperty, 0.00);
                    //trackingMouseMove = false;
                }
                else if (newLeft < -System.Math.Abs(element.Width - ControlWidth))
                {
                    element.SetValue(Canvas.LeftProperty, -System.Math.Abs(element.Width - ControlWidth));
                    //trackingMouseMove = false;
                }
                mousePosition = e.GetPosition(null);
               
               
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值