我如何放大&缩小图像并移动所有图像

时间:2016-06-08 11:16:30

标签: c# uwp-xaml

我在画布上显示图像,我希望能够移动所有图像。目前我只能移动我带到画布上的最后一张图片。我也希望能够缩放这些图像。请帮助我。

private TranslateTransform dragTranslation;

// Constructor
void Drag_ManipulationDelta(object sender,ManipulationDeltaRoutedEventArgs e)
{
    // Move the rectangle.

    dragTranslation.X += e.Delta.Translation.X;

    dragTranslation.Y += e.Delta.Translation.Y;
}                                                                      


private void Stickers1_SelectionChanged(object sender,SelectionChangedEventArgs e)
{

    var selecteditem = e.AddedItems[0] as StickersImageListModel;

    Stickers1.Visibility = Visibility.Collapsed;

    Image imageitem = new Image();

    BitmapImage image = new BitmapImage(new System.Uri(selecteditem.Imageurl, UriKind.Absolute));

    imageitem.Source = image;

    my_canvas.Children.Add(imageitem);

    imageitem.AllowDrop = true;

    imageitem.ManipulationMode = ManipulationModes.All;

    imageitem.ManipulationDelta += Drag_ManipulationDelta;

    dragTranslation = new TranslateTransform();

    imageitem.RenderTransform = this.dragTranslation;

    var st = (ScaleTransform)imageitem.RenderTransform;

    double zoom = e.Delta > 0 ? .2 : -.2;

    st.ScaleX += zoom;

    st.ScaleY += zoom;

    my_canvas.Visibility = Visibility.Visible;

}

1 个答案:

答案 0 :(得分:0)

您不必使用dragTranslation属性

void Drag_ManipulationDelta(object sender,ManipulationDeltaRoutedEventArgs e)
{
    // Move the rectangle.

    Image img = sender as Image;
    CompositeTransform ct = img.RenderTransform as CompositeTransform;
    ct.ScaleX *= e.Delta.Scale;
    ct.ScaleY *= e.Delta.Scale;

    if (ct.ScaleX < 1.0) ct.ScaleX = 1.0;
    if (ct.ScaleY < 1.0) ct.ScaleY = 1.0;
    if (ct.ScaleX > 4.0) ct.ScaleX = 4.0;
    if (ct.ScaleY > 4.0) ct.ScaleY = 4.0;
    //Checking with canvas boundary so that image wont go behind canvas
    if ((ct.TranslateX + e.Delta.Translation.X) <= (mycanvas.ActualWidth - img.ActualWidth) && ct.TranslateX + e.Delta.Translation.X>=0)
         ct.TranslateX += e.Delta.Translation.X;
    if ((ct.TranslateY + e.Delta.Translation.Y) <= (mycanvas.ActualHeight - img.ActualHeight) && ct.TranslateY + e.Delta.Translation.Y >= 0)
         ct.TranslateY += e.Delta.Translation.Y;    
} 

private void Stickers1_SelectionChanged(object sender,SelectionChangedEventArgs e)
{        
    ...
    //Using CompositeTransform instead of TranslateTransform
    CompositeTransform ct = new CompositeTransform();
    imageitem.RenderTransform = ct;    
}