在 WP8.1 中如何放大图片

6

我有一个全屏幕的图片,使用以下代码实现:

  <phone:PhoneApplicationPage
      x:Class="solution.FullScreenViewer"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
      xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      FontFamily="{StaticResource PhoneFontFamilyNormal}"
      FontSize="{StaticResource PhoneFontSizeNormal}"
      Foreground="{StaticResource PhoneForegroundBrush}"
      SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
      mc:Ignorable="d"
      shell:SystemTray.IsVisible="True">


      <Image Name="img" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"Stretch="Uniform"/>

  </phone:PhoneApplicationPage>

这只是一个更大项目的一部分。我想实现在单击图像后全屏打开图像的功能,因此我制作了另一个页面,只有一个图像,没有其他内容。我正在使用以下代码从C#加载图像:

  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
      string context = this.NavigationContext.QueryString["context"];
      img.Source = new BitmapImage(new Uri(context, UriKind.RelativeOrAbsolute));
      base.OnNavigatedTo(e);
  }

现在我想要添加一个图片缩放选项,但是我不知道该怎么做。我尝试了谷歌搜索,但是我唯一找到的方法是在ScroolViewer中使用ZoomMode,但是这对我不起作用(它说成员ZoomMode未被识别)。是否有其他缩放方案?

2
由于您的目标是 SilverLight - 您是否尝试过其中之一?one, two, three - Romasz
2个回答

7

您可以使用一个包含内部Grid的Grid替代当前使用的图像。在第二个Grid中,使用Grid.RenderTransform来使用缩放变换调整其内容(网格中的图像)。

您可以使用ManipulationDelta事件来跟踪何时缩放。

这样,您只需放大图片即可,但这并不是很好,因为您只关注图像的左上角。为了避免这种情况,您可以通过在图像渲染变换标记中添加平移变换来使用户滚动图像。您可以参考以下代码:

<Grid x:Name="LayoutRoot" ManipulationDelta="LayoutRoot_ManipulationDelta">
    <Grid x:Name="ContentPanel">
        <Grid x:Name="imageGrid">
            <Grid.RenderTransform>
                <ScaleTransform x:Name="ImageTransform" />
            </Grid.RenderTransform>
            <Image x:Name="img" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Stretch="Uniform" Source="/Resources/Logo/CTK .png"
                ManipulationDelta="img_ManipulationDelta"
                ManipulationCompleted="img_ManipulationCompleted">
                <Image.RenderTransform>
                    <TranslateTransform x:Name="PanTransform"/>
                </Image.RenderTransform>
                <Image.Resources>
                    <Storyboard x:Name="Pan">
                        <DoubleAnimation x:Name="PanAnimation"
                        Storyboard.TargetName="PanTransform"
                        Storyboard.TargetProperty="X" Duration="0:0:1">
                            <DoubleAnimation.EasingFunction>
                                <CircleEase EasingMode="EaseOut" />
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </Image.Resources>
            </Image>
        </Grid>
    </Grid>
</Grid>

以下是用于缩放和平移的 C# 代码:

    private void LayoutRoot_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
    {
        if (e.DeltaManipulation.Scale.X > 0.0 && e.DeltaManipulation.Scale.Y > 0.0)
        {
            // Scale in the X direction
            double tmp = ImageTransform.ScaleX * ((e.DeltaManipulation.Scale.X + e.DeltaManipulation.Scale.Y) / 2);

            if (tmp < 1.0)
                tmp = 1.0;
            else if (tmp > 4.0)
                tmp = 4.0;

            ImageTransform.ScaleX = tmp;

            // Scale in the Y direction
            tmp = ImageTransform.ScaleY * ((e.DeltaManipulation.Scale.X + e.DeltaManipulation.Scale.Y) / 2);

            if (tmp < 1.0)
                tmp = 1.0;
            else if (tmp > 4.0)
                tmp = 4.0;

            ImageTransform.ScaleY = tmp;
        }
    }

    private void img_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
    {
        // First make sure we're translating and not scaling (one finger vs. two)
        if (e.DeltaManipulation.Scale.X == 0.0 && e.DeltaManipulation.Scale.Y == 0.0)
        {
            Image photo = sender as Image;
            TranslateTransform transform = photo.RenderTransform as TranslateTransform;

            // Compute the new X component of the transform
            double x = transform.X + e.DeltaManipulation.Translation.X;
            double y = transform.Y + e.DeltaManipulation.Translation.Y;

            // Apply the computed value to the transform
            transform.X = x;
            transform.Y = y;
        }
    }

    private void img_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
    {
        if (e.IsInertial)
        {
            Image photo = sender as Image;

            // Compute the inertial distance to travel
            double dx = e.FinalVelocities.LinearVelocity.X / 10.0;
            double dy = e.FinalVelocities.LinearVelocity.Y / 10.0;
            TranslateTransform transform = photo.RenderTransform as TranslateTransform;

            double x = transform.X + dx;
            double y = transform.Y + dy;

            // Apply the computed value to the animation
            PanAnimation.To = x;

            // Trigger the animation
            Pan.Begin();
        }
    } 

谢谢你详细的回答,我现在会尝试一下 :) - user2700896

0

你应该能够像这样在应用程序级别上进行缩放:

public static void ZoomToRatio(double ratio)
{
   ScaleTransform transform = new ScaleTransform()
   {
      ScaleX = ratio,
      ScaleY = ratio,
   };

   double height = 0.0;
   double width = 0.0;

   if (ratio < 1.0)
   {
      height = Application.Current.Host.Content.ActualHeight * ratio;
      width = Application.Current.Host.Content.ActualWidth * ratio;
   }
   else
   {
      height = Application.Current.Host.Content.ActualHeight / ratio;
      width = Application.Current.Host.Content.ActualWidth / ratio;          
   }

   Application.Current.RootVisual.SetValue(Canvas.HeightProperty, height);
   Application.Current.RootVisual.SetValue(Canvas.WidthProperty, width);

   Application.Current.RootVisual.RenderTransform = transform;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接