none
图片缩放的问题 RRS feed

  • 问题

  • http://social.msdn.microsoft.com/Forums/zh-CN/metroappzhcn/thread/d36d0b68-1e3e-4a38-8400-648a3807e404
     
    按照上面这个帖子中的john厦门的方法,就用一张图片放进去试了下失败了。不知道是什么原因呢?有人用这个方法做出来的吗?


    另外我用scrollviewer配合flipviewer已经实现了图片集的浏览和缩放,但是现在面临的一个问题是,我在第一张图片缩放之后,点击flipviewer向右之后再向左返回第一张图片,呈现给我的还是缩放之后的大小,我期望呈现给我的是原始大小的图片。但是scrollviewer里面的zoomfactor是一个只读类型,不能对其赋值。这个问题应该怎么解决呢?

    2012年8月13日 8:15

答案

  • 使用 http://social.msdn.microsoft.com/Forums/zh-CN/metroappzhcn/thread/d36d0b68-1e3e-4a38-8400-648a3807e404 里面的方法所要实现的代码如下:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <FlipView ItemsSource="{Binding}" SelectionChanged="FlipView_SelectionChanged">
                <FlipView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Image x:Name="img" Source="{Binding Image}" 
                                   ManipulationMode="Scale" RenderTransformOrigin="0.5,0.5"
                                   ManipulationDelta="img_ManipulationDelta">
                                <Image.RenderTransform>
                                    <CompositeTransform x:Name="imageTransform" />
                                </Image.RenderTransform>
                            </Image>
                        </Grid>
                    </DataTemplate>
                </FlipView.ItemTemplate>
            </FlipView>
        </Grid>

    C#:

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                List<ImageItem> items = new List<ImageItem>();
                for (int i = 1; i <= 6; i++)
                {
                    BitmapImage image = new BitmapImage();
                    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Assets/img" + i + ".jpg"));
                    image.SetSource(await file.OpenAsync(FileAccessMode.Read));
                    items.Add(new ImageItem() { Image = image });
                }
                this.DataContext = items;
            }
            private void img_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
            {
                Image img = sender as Image;
                CompositeTransform imageTransform = img.FindName("imageTransform") as CompositeTransform;
                // Scale the photo
                imageTransform.ScaleX *= e.Delta.Scale;
                imageTransform.ScaleY *= e.Delta.Scale;
                // Constrain scale factor
                imageTransform.ScaleX = Math.Min(imageTransform.ScaleX, 5.0);
                imageTransform.ScaleY = Math.Min(imageTransform.ScaleY, 5.0);
                imageTransform.ScaleX = Math.Max(imageTransform.ScaleX, 0.5);
                imageTransform.ScaleY = Math.Max(imageTransform.ScaleY, 0.5);
                e.Handled = false;
            }
            private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                FlipView flipView = sender as FlipView;
                if (e.AddedItems != null && e.AddedItems.Count == 1)
                {
                    FlipViewItem item = flipView.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]) as FlipViewItem;
                    if (item != null)
                    {
                        Image img = FindVisualChildByName<Image>(item, "img");
                        (img.RenderTransform as CompositeTransform).ScaleX = 1;
                        (img.RenderTransform as CompositeTransform).ScaleY = 1;
                    }
                }
            }
            public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    string controlName = child.GetValue(Control.NameProperty) as string;
                    if (controlName == name)
                    {
                        return child as T;
                    }
                    else
                    {
                        T result = FindVisualChildByName<T>(child, name);
                        if (result != null)
                            return result;
                    }
                }
                return null;
            }
        }
        public class ImageItem
        {
            public BitmapImage Image { get; set; }
        }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 neal_young 2012年8月15日 2:03
    2012年8月14日 5:40
    版主