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>

  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);}}
}

  详细说明:http://wp.662p.com/thread-8171-1-1.html

转载于:https://www.cnblogs.com/liniuzen/p/3985774.html

WindowsPhone8可缩放图片控件的实现相关推荐

  1. android控件触摸缩放,Android控件之ZoomControls缩放使用

    先看一下效果 正常 缩小 放大 一.简介 ZoomControls是一组可缩放的控件.它包含俩个按钮(放大按钮.缩小按钮) 二.重要方法 hasFocus():判断焦点 hide():隐藏 onTou ...

  2. html5 比例尺,高德地图API之缩放比例尺控件+3D转换

    缩放比例尺控件 首先引入控件 amap.scale 然后使用 map.addcontrol() 添加控件 map *{margin:0;padding:0;list-style: none;} #co ...

  3. MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片

    最近有同学问我如何实现MFC基于对话框在图片控件中加载图片?其实使用MFC显示图片的方法各种各样,但是还是有些同学不知道怎样显示.以前在<数字图像处理>课程中完成的软件都是基于单文档的程序 ...

  4. VS2013 MFC 直接将 OpenCV2.0/3.0 库中的 Mat 结构的图像传递到 Picture Control(图片控件)

    接上文 VS2013 MFC + OpenCV3.0 打开图片: 既然我们已经从 OpenCV1.0 走到了 OpenCV2.0 乃至更高,又何苦在写基于 MFC 的图像处理程序时,又回到 OpenC ...

  5. 一步步学习微软InfoPath2010和SP2010--第十二章节--管理和监控InfoPath Form Services(IPFS)(4)--监控含图片控件的Products表单...

    如本章节前面提到的,你的IPFS表单表现没有你想象的好有很多可能的原因.一个最明显的原因是表单产生太多通信量(因为表单产生的HTML的大小).在许多你使用了大型.笨拙表单的许多方法中,最常见的是让用户 ...

  6. Android图片控件,跟随列表(recyclerView)的上下滚动而同步平移。

    一个用于放置在RecycleView中的图片控件,其主要功能是跟随列表的上下滚动而上下平移,使得呈现出一种图像相对列表静止的感觉. Overview ScrollingImageView 提供以下特性 ...

  7. 【Android之SmartImageView图片控件】

    源码地址是https://github.com/loopj/android-smart-image-view,没有sample,本文最后会提供一个sample. smartimageview提供的主要 ...

  8. 安卓圆形图片控件CircleImageView的使用

    CircleImageView的使用 ​ 在安卓开发中,我们常常会碰到一些圆形图片控件的困扰,而选择一款方便的圆形图片控件便成为了提高开发效率的有效手段,下面我推荐一款github上的项目,也是我本人 ...

  9. 高德地图-缩放比例尺控件

    缩放比例尺控件 添加插件:plugin=AMap.scale,AMap.ToolBar; AMap.scale 比例尺插件.位于地图右下角,用户可控制其显示与隐藏. map.addControl(ne ...

最新文章

  1. python主成分分析相关系数_python如何进行主成分分析
  2. Hover States - 有趣的用户界面及交互设计
  3. git stash和git stash pop
  4. ON REG EXPRESSION.SYNTAX
  5. 添加右键用Sublime Text3 打开文件和文件夹
  6. JAVA Map 和 List 排序方法
  7. 内固定取出术后护理_“钢铁侠“们注意了——身体内的钢板或内固定需要取出吗?...
  8. 消息称Uber正洽谈出售旗下自动驾驶部门ATG给Aurora
  9. Weblogic负载均衡/Session复制之集群架构续
  10. java单个变量的表达式_java中使用Lambda表达式的5种语法
  11. 数据库得事务控制详解,什么是事务回滚详解,通俗易懂
  12. roller源码分析
  13. 商城购物系统软件测试,网上商城购物系统黑盒测试
  14. 云盒子linux版本,10分钟,搭建好属于自己的私有云盘系统
  15. wallpaper 壁纸提取
  16. 每天睡6小时和8小时的区别 看完再不敢熬夜了
  17. 使用ffmpeg将m3u8文件转为mp4
  18. 基于关联规则(Apriori)+协同过滤(collaborative filtering)实现电影推荐系统
  19. 机器取代人的智造年代,你靠什么掌舵?
  20. Shell:用sed命令删除特定行

热门文章

  1. Spring Boot 集成 WebSocket通信信息推送!
  2. 开发工具:IDEA 强大的 Live Templates!
  3. JavaScript框架的超简史
  4. 真正聪明的人,为什么从不去社交?
  5. python 更新数据库历史_python 实现数据库中数据添加、查询与更新的示例代码
  6. 提现接口网站 php,API提现接口
  7. 面试体验:Facebook 篇(转)
  8. Laravel 测试: PHPUnit 入门教程
  9. Multicast注册中心
  10. 一行命令 优化上传速度