gpt4 book ai didi

c# - 如何在 inkcanvas 上同时进行缩放和旋转?

转载 作者:行者123 更新时间:2023-11-30 18:05:15 27 4
gpt4 key购买 nike

使用以下 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# - 如何在 inkcanvas 上同时进行缩放和旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com