C# 在WP8.1中缩放图像

C# 在WP8.1中缩放图像,c#,xaml,windows-phone,windows-phone-8.1,C#,Xaml,Windows Phone,Windows Phone 8.1,我有一个使用以下代码的全屏图像: <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-na

我有一个使用以下代码的全屏图像:

  <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>
  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的成员无法识别)


是否有其他放大解决方案?

您应该能够以如下方式放大应用程序:

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;
}
publicstaticvoidzoomtoratio(双倍比率)
{
ScaleTransform转换=新的ScaleTransform()
{
ScaleX=比率,
ScaleY=比率,
};
双倍高度=0.0;
双倍宽度=0.0;
如果(比率<1.0)
{
高度=Application.Current.Host.Content.ActualHeight*比率;
宽度=Application.Current.Host.Content.ActualWidth*比率;
}
其他的
{
高度=Application.Current.Host.Content.ActualHeight/比率;
宽度=Application.Current.Host.Content.ActualWidth/比率;
}
Application.Current.RootVisual.SetValue(Canvas.HeightProperty,height);
Application.Current.RootVisual.SetValue(Canvas.WidthProperty,width);
Application.Current.RootVisual.RenderTransform=转换;
}

您可以使用一个内部有另一个网格的网格,而不是正在使用的图像。在第二个网格上,使用
grid.RenderTransform
通过缩放变换调整网格内容(网格内的图像)的大小。 您可以使用
operationdelta
事件跟踪何时放大或缩小

使用它,你可以把图片放大,但这并不是很好,因为你聚焦在图片的左上角。为了避免这种情况,您可以通过在“图像渲染变换”标记中添加“平移变换”,使用户能够滚动图像。您可以在下面的代码中看到如何做到这一点:

<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();
        }
    } 
private void LayoutRoot\u操纵delta(对象发送方,System.Windows.Input.操纵deltaeventargs e)
{
if(e.DeltaManipulation.Scale.X>0.0&&e.DeltaManipulation.Scale.Y>0.0)
{
//在X方向上缩放
double tmp=ImageTransform.ScaleX*((e.DeltaManipulation.Scale.X+e.DeltaManipulation.Scale.Y)/2);
如果(tmp<1.0)
tmp=1.0;
否则如果(tmp>4.0)
tmp=4.0;
ImageTransform.ScaleX=tmp;
//在Y方向上缩放
tmp=ImageTransform.ScaleY*((e.DeltaManipulation.Scale.X+e.DeltaManipulation.Scale.Y)/2);
如果(tmp<1.0)
tmp=1.0;
否则如果(tmp>4.0)
tmp=4.0;
ImageTransform.ScaleY=tmp;
}
}
私有无效img_操纵增量(对象发送方,System.Windows.Input.ManipulationDeltaEventArgs e)
{
//首先确保我们正在平移,而不是缩放(一个手指与两个手指)
if(e.DeltaManipulation.Scale.X==0.0&&e.DeltaManipulation.Scale.Y==0.0)
{
图像照片=发送者作为图像;
TranslateTransform transform=photo.RenderTransform作为TranslateTransform;
//计算变换的新X分量
double x=transform.x+e.DeltaManipulation.Translation.x;
双y=transform.y+e.DeltaManipulation.Translation.y;
//将计算出的值应用于变换
变换X=X;
变换Y=Y;
}
}
私有无效img_操作已完成(对象发送方,System.Windows.Input.OperationCompletedEventArgs e)
{
如果(例如,IsInertial)
{
图像照片=发送者作为图像;
//计算行驶的惯性距离
双dx=e.finalvelocity.LinearVelocity.X/10.0;
双dy=e.finalvelocity.LinearVelocity.Y/10.0;
TranslateTransform transform=photo.RenderTransform作为TranslateTransform;
双x=变换.x+dx;
双y=transform.y+dy;
//将计算出的值应用于动画
PanAnimation.To=x;
//触发动画
Pan.Begin();
}
} 

由于您的目标是SilverLight,您尝试过其中一种吗,谢谢你的详细回答,我现在就试试:)