我怎样才能做到既缩放和旋转上inkcanvas?

使用下面的XAML:

<Grid x:Name="grid" Background="LightBlue" ClipToBounds="True"> <Grid.RowDefinitions> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Viewbox x:Name="imgViewbox" > <InkCanvas Grid.Row="0" Name="inkCanvas" Background="Red" > <Image Source="Images/pic.png" HorizontalAlignment="Left" x:Name="imgObject" VerticalAlignment="Top" /> <Label>Testing</Label> </InkCanvas> </Viewbox> </Grid>

我想围绕图像的中心旋转,还可以使用鼠标滚轮来放大。 我已经建立了这种变换组和事件:

public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); transformGroup = new TransformGroup(); scaleTransform = new ScaleTransform(); rotateTransform = new RotateTransform(); translateTransform = new TranslateTransform(); transformGroup.Children.Add(rotateTransform); transformGroup.Children.Add(scaleTransform); transformGroup.Children.Add(translateTransform); imgViewbox.RenderTransform = transformGroup; imgViewbox.MouseWheel += ImageViewboxMouseWheel; }

旋转很简单:

void Rotate(object sender, RoutedEventArgs e) { //imgViewbox.RenderTransformOrigin = new Point(0.5,0.5); rotateTransform.Angle += 90; }

但变焦在做各种怪异的东西在屏幕上跳跃。 变焦的代码是在这里:

void ImageViewboxMouseWheel(object sender, MouseWheelEventArgs e) { //imgViewbox.RenderTransformOrigin = new Point(0, 0); double zoomFactor = DefaultZoomFactor; if (e.Delta <= 0) zoomFactor = 1.0 / DefaultZoomFactor; // DoZoom requires both the logical and physical location of the mouse pointer var physicalPoint = e.GetPosition(imgViewbox); if (transformGroup.Inverse != null) { DoZoom(zoomFactor, transformGroup.Inverse.Transform(physicalPoint), physicalPoint); } else { throw new ArgumentException("Missing Inverse"); } //Set the center point of the ScaleTransform object to the cursor location. scaleTransform.CenterX = e.GetPosition(imgViewbox).X; scaleTransform.CenterY = e.GetPosition(imgViewbox).Y; Debug.WriteLine(string.Format("IVMW Center {0},{1}", scaleTransform.CenterX, scaleTransform.CenterY)); } public void DoZoom(double deltaZoom, Point mousePosition, Point physicalPosition) { double currentZoom = scaleTransform.ScaleX; currentZoom *= deltaZoom; translateTransform.X = -1*(mousePosition.X*currentZoom - physicalPosition.X); translateTransform.Y = -1*(mousePosition.X*currentZoom - physicalPosition.Y); scaleTransform.ScaleX = currentZoom; scaleTransform.ScaleY = currentZoom; }

我已删除的,尽我所能,动画和这样的。 只希望留下关键部分。 我认为,主要问题是scaleTransform.Center [X | Y]作为被返回遍布象限甚至当我试图在相同的位置点击准确的数字。 该RenderTransformOrigin似乎并没有做出与中心位置的差异,但我知道我需要它围绕中心旋转。

我究竟做错了什么?

--------------解决方案-------------

你需要弥补你在TranslateTransform改变ScaleTranform的的centerX / Y这一跳得到,这里是从平移和缩放控制我写了一个片段:

private void This_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (IsZoomEnabled)
{
Point cursorPos = e.GetPosition(this);
Point newCenter = _scaleT.Inverse.Transform(_translateT.Inverse.Transform(cursorPos));
Point oldCenter = new Point(_scaleT.CenterX, _scaleT.CenterY);
Vector oldToNewCenter = newCenter - oldCenter;
_scaleT.CenterX = newCenter.X;
_scaleT.CenterY = newCenter.Y;
_translateT.X += oldToNewCenter.X * (_scaleT.ScaleX - 1.0);
_translateT.Y += oldToNewCenter.Y * (_scaleT.ScaleY - 1.0);
...

希望你能适应这你的代码。 在那里计算新的中心可能需要把你的RotateTransform考虑。

分类:C# 时间:2015-03-15 人气:1
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.374 (s). 10 q(s)