微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在WP8.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-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>

这只是一个更大项目的一部分.我想在点击它之后实现te feture以全屏打开图像,所以我用另一个图像制作了另一个页面.我正在使用以下代码从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无法识别).

还有其他解决方案可以放大吗?

解决方法

您可以使用其中包含另一个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();
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


原文地址:http://msdn.microsoft.com/en-us/magazine/cc163791.aspx 原文发布日期: 9/19/2005 原文已经被 Microsoft 删除了,收集过程中发现很多文章图都不全,那是因为原文的图都不全,所以特收集完整全文。 目录 前言 CLR启动程序
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采纳和使用,它的确提供了很多的优势也解决了很多的问题,但是我们也知道也并不是银弹,提供优势的同时它也给我们的开发人员和团队也带来了很多的挑战。 为了迎接或者采用这些新技术,开发团队需要更加注重一些流程或工具的使用,这样才能更好的适应这些新技术所
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下PLINQ中的分区。上一篇介绍了并行编程,这边详细介绍一下并行编程中的分区和自定义分区。 先做个假设,假设我们有一个200Mb的文本文件需要读取,怎么样才能做到最优的速度呢?对,很显然就是拆分,把文本文件拆分成很多个小文件,充分利用我们计算机中
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Microsoft为了利用这个硬件特性,于是在Visual Studio 2010 和 .NET Framework 4的发布及以上版本中,添加了并行编程这个新特性,我想它以后势必会改变我们的开发方式。 在以前或者说现在,我们在并行开发的时候可
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么
c语言怎么求字符串的长度并输出
c语言函数的三种调用方式是什么
c语言中保留两位小数怎么表示
double的输入格式符是什么