2013-02-07 42 views
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; 
    } 

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

回答

0

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

这是一个非常简单的例子

XAML

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

我曾与比例系数相乘,我通过增加它做了一个错误距离,感谢你.. –