wpf下实现图片的放大缩小和平移

参考文章:http://blog.csdn.net/hsienhua/article/details/7859191
//-------------------------------------------------------------------------------------------------

 
  

原文中在小于原尺寸时直接退出了,额... 原文是以光标为中心缩放,但是在小于原尺寸的时候,情况不乐观。 下面的函数是以图片为中心进行缩放,不会出现跳跃的情况 ,亲测可用!

//curPoint : e.GetPosition(UIElement) private void DowheelZoomEx(TransformGroup group, double scale, Point curPoint) { ScaleTransform transform = group.Children[0] as ScaleTransform; TranslateTransform transform1 = group.Children[1] as TranslateTransform; #if IMAGE_CENTER if (transform.ScaleX + scale < 1) { //return; } transform.ScaleX += scale; transform.ScaleY += scale; double nW = Image1.ActualWidth; double nH = Image1.ActualHeight; transform1.X -= scale * nW / 2; transform1.Y -= scale * nH / 2; #else //以光标所在点为中心进行放大缩小 double deltaScale = 0.0; if (delta > 0) { deltaScale = transform.ScaleX * 0.1; if (Math.Abs(transform.ScaleX + deltaScale) >= 16.0) { return; } } else { deltaScale = transform.ScaleX * (-0.1); if (Math.Abs(transform.ScaleX + deltaScale) <= 0.2) { return; } } transform.CenterX = nW / 2; transform.CenterY = nH / 2; transform.ScaleX += deltaScale; transform.ScaleY = Math.Abs(transform.ScaleX); transform1.X += deltaScale * (transform.CenterX - point.X); if (transform.ScaleX > 0.0) { transform1.Y += deltaScale * (transform.CenterY - point.Y); } else { transform1.Y -= deltaScale * (transform.CenterY - point.Y); } #endif }

你可能感兴趣的:(WPF)