WP--图片缩放


1.xaml:

<ViewportControl x:Name="viewport" DoubleTap="OnDoubleTap"
        ManipulationStarted="OnManipulationStarted" ManipulationDelta="OnManipulationDelta" 
                     ManipulationCompleted="OnManipulationCompleted" ViewportChanged="viewport_ViewportChanged">
    <Canvas x:Name="canvas">
        <Image x:Name="image" 
                    RenderTransformOrigin="0,0" CacheMode="BitmapCache"
                   ImageOpened="OnImageOpened">
            <Image.RenderTransform>
                <ScaleTransform x:Name="xform"/>
            </Image.RenderTransform>
        </Image>
    </Canvas>
</ViewportControl>

2. cs:

namespace ImageExtend
{
    public partial class ZoomImage : UserControl
    {
        public static readonly DependencyProperty SourceProperty
            = DependencyProperty.Register("Source", typeof(ImageSource), typeof(ZoomImage), new PropertyMetadata(OnImageSourceChanged));
        private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d != null && d is ZoomImage)
            {
                (d as ZoomImage).SetImage((ImageSource)e.NewValue);
            }
        }
        public ImageSource Source
        {
            get
            {
                return (ImageSource)GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }
 
        const double MaxScale = 10;
 
        double _scale = 1.0;
        double _minScale;
        double _coercedScale;
        double _originalScale;
 
        Size _viewportSize;
        bool _pinching;
        Point _screenMidpoint;
        Point _relativeMidpoint;
 
        BitmapImage _bitmap;
 
 
        public ZoomImage()
        {
            InitializeComponent();
            this.Loaded += ZoomImage_Loaded;
        }
 
        void ZoomImage_Loaded(object sender, RoutedEventArgs e)
        {
            if (Source != null)
            {
                SetImage(Source);
            }
        }
 
        void SetImage(ImageSource img)
        {
            image.Source = img;
        }
 
        /// <summary> 
        /// Either the user has manipulated the image or the size of the viewport has changed. We only 
        /// care about the size. 
        /// </summary> 
        void viewport_ViewportChanged(object sender, System.Windows.Controls.Primitives.ViewportChangedEventArgs e)
        {
            Size newSize = new Size(viewport.Viewport.Width, viewport.Viewport.Height);
            if (newSize != _viewportSize)
            {
                _viewportSize = newSize;
                CoerceScale(true);
                ResizeImage(false);
            }
        }
 
        /// <summary> 
        /// Handler for the ManipulationStarted event. Set initial state in case 
        /// it becomes a pinch later. 
        /// </summary> 
        void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            _pinching = false;
            _originalScale = _scale;
        }
 
        /// <summary> 
        /// Handler for the ManipulationDelta event. It may or may not be a pinch. If it is not a  
        /// pinch, the ViewportControl will take care of it. 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            if (e.PinchManipulation != null)
            {
                e.Handled = true;
 
                if (!_pinching)
                {
                    _pinching = true;
                    Point center = e.PinchManipulation.Original.Center;
                    _relativeMidpoint = new Point(center.X / image.ActualWidth, center.Y / image.ActualHeight);
 
                    var xform = image.TransformToVisual(viewport);
                    _screenMidpoint = xform.Transform(center);
                }
 
                _scale = _originalScale * e.PinchManipulation.CumulativeScale;
 
                CoerceScale(false);
                ResizeImage(false);
            }
            else if (_pinching)
            {
                _pinching = false;
                _originalScale = _scale = _coercedScale;
            }
        }
 
        /// <summary> 
        /// The manipulation has completed (no touch points anymore) so reset state. 
        /// </summary> 
        void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            _pinching = false;
            _scale = _coercedScale;
        }
 
 
        /// <summary> 
        /// When a new image is opened, set its initial scale. 
        /// </summary> 
        void OnImageOpened(object sender, RoutedEventArgs e)
        {
            _bitmap = (BitmapImage)image.Source;
 
            // Set scale to the minimum, and then save it. 
            _scale = 0;
            CoerceScale(true);
            _scale = _coercedScale;
 
            ResizeImage(true);
        }
 
        /// <summary> 
        /// Adjust the size of the image according to the coerced scale factor. Optionally 
        /// center the image, otherwise, try to keep the original midpoint of the pinch 
        /// in the same spot on the screen regardless of the scale. 
        /// </summary> 
        /// <param name="center"></param> 
        void ResizeImage(bool center)
        {
            if (_coercedScale != 0 && _bitmap != null)
            {
                double newWidth = canvas.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
                double newHeight = canvas.Height = Math.Round(_bitmap.PixelHeight * _coercedScale);
 
                xform.ScaleX = xform.ScaleY = _coercedScale;
 
                viewport.Bounds = new Rect(0, 0, newWidth, newHeight);
 
                if (center)
                {
                    viewport.SetViewportOrigin(
                        new Point(
                            Math.Round((newWidth - viewport.ActualWidth) / 2),
                            Math.Round((newHeight - viewport.ActualHeight) / 2)
                            ));
                }
                else
                {
                    Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                    Point origin = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                    viewport.SetViewportOrigin(origin);
                }
            }
        }
 
        /// <summary> 
        /// Coerce the scale into being within the proper range. Optionally compute the constraints  
        /// on the scale so that it will always fill the entire screen and will never get too big  
        /// to be contained in a hardware surface. 
        /// </summary> 
        /// <param name="recompute">Will recompute the min max scale if true.</param> 
        void CoerceScale(bool recompute)
        {
            if (recompute && _bitmap != null && viewport != null)
            {
                // Calculate the minimum scale to fit the viewport 
                double minX = viewport.ActualWidth / _bitmap.PixelWidth;
                double minY = viewport.ActualHeight / _bitmap.PixelHeight;
 
                _minScale = Math.Min(minX, minY);
            }
 
            _coercedScale = Math.Min(MaxScale, Math.Max(_scale, _minScale));
 
        }
 
        private void OnDoubleTap(object sender, GestureEventArgs e)
        {
            e.Handled = true;
 
            _scale = 0;
            CoerceScale(true);
            _scale = _coercedScale;
 
            ResizeImage(true);
        }
    }
}

DLL下载

使用方法

1:添加ImageExtend引用

2:xmlns:ImageExtend="clr-namespace:ImageExtend;assembly=ImageExtend"

3:<ImageExtend:CircleImage Source="test.jpg" Width="400" Height="400"/>


WP-REST-API 是WordPress 的一种接口,它通过提供标准化的RESTful API,允许开发人员使用HTTP请求来访问和操作WordPress站点的内容和数据。通过这个接口,开发人员可以使用不同的编程语言和技术来与WordPress进行交互,从而使得开发更加灵活和自由。 JWT(JSON Web Token)是一种用于认证和授权的开放标准。它通过将用户信息和权限信息编码成一种加密的令牌,以实现跨服务器和跨域的身份验证。JWT 是由三部分组成的:头部、负载和签名。头部包含令牌的加密算法和类型信息,负载包含用户的相关信息,签名用于验证令牌的真实性和完整性。 WP-REST-API JWT整合了WordPress的REST API和JWT的认证机制,使得在使用WP-REST-API进行开发的过程中,可以增加身份验证和授权的功能。它允许开发人员在请求WordPress REST API时,通过在请求头或参数中提供有效的JWT令牌来验证用户的身份和权限,并根据令牌中的负载信息来进行授权。 WP-REST-API JWT的使用具有很多优势。首先,它提供了一种轻量级的身份验证方式,减少了开发的复杂性。其次,通过JWT令牌的机制,可以实现无状态的认证和授权,提高了性能和可扩展性。此外,JWT还提供了一种可靠的机制来防止伪造和篡改请求数据,增强了系统的安全性。 总而言之,WP-REST-API JWT为开发人员提供了一种方便、灵活和安全的方式来使用WordPress的REST API。它简化了身份验证和授权的过程,并通过使用JWT令牌提高了系统的性能和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值