2

我正在尝试裁剪图像。为了选择裁剪区域,我使用了一个矩形元素。矩形的初始宽度和高度分别设置为 100。捏合时矩形的大小将增加。我怎样才能获得这个被放大的矩形的大小?

我正在使用的代码如下:

    private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)  
    {                                                     
         ImageTransformation.ScaleX = _initialScale * e.DistanceRatio;   
         ImageTransformation.ScaleY = ImageTransformation.ScaleX;   
         cropRectangle.Width = cropRectangle.Width + e.DistanceRatio;  
         cropRectangle.Height = cropRectangle.Height + e.DistanceRatio;  
    }

我无法获得放大矩形的大小

4

1 回答 1

0

RenderTransforms 不会改变控件的宽度和高度,因为它只影响渲染。不确定是否有更好的方法,但您可以使用原始大小和比例因子简单地计算渲染大小。

这是一个非常基本的例子

xml

<Canvas x:Name="cnv" Grid.Row="1">      
    <Rectangle Canvas.Top="150" Canvas.Left="150" 
               Width="200" Height="200" x:Name="myrect" 
               Fill="AliceBlue" 
               ManipulationStarted="myrect_ManipulationStarted" 
               ManipulationDelta="myrect_ManipulationDelta" 
               ManipulationCompleted="myrect_ManipulationCompleted">             
    </Rectangle>
</Canvas>

代码

public MainPage()
{
    InitializeComponent();

    transformGroup = new TransformGroup();
    translation = new TranslateTransform();
    scale = new ScaleTransform();
    transformGroup.Children.Add(scale);
    transformGroup.Children.Add(translation);
    myrect.RenderTransform = transformGroup;
}

private TransformGroup transformGroup;
private TranslateTransform translation;
private ScaleTransform scale;

private void myrect_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    //change the color of the Rectangle to a half transparent Red
    myrect.Fill = new SolidColorBrush(Color.FromArgb(127, 255, 0, 0));
}

private void myrect_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    translation.X += e.DeltaManipulation.Translation.X;
    translation.Y += e.DeltaManipulation.Translation.Y;

    //Scale the Rectangle
    if (e.DeltaManipulation.Scale.X != 0)
        scale.ScaleX *= e.DeltaManipulation.Scale.X;
    if (e.DeltaManipulation.Scale.Y != 0)
        scale.ScaleY *= e.DeltaManipulation.Scale.Y;

}

private void myrect_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
    myrect.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
    Debug.WriteLine(myrect.Width * scale.ScaleX);
    Debug.WriteLine(myrect.Height * scale.ScaleY);
}
于 2013-02-08T08:22:54.090 回答