Metro风格应用程序中的XAML图像质量(插值)

时间:2012-07-08 17:08:28

标签: datatemplate microsoft-metro winrt-xaml

给定以下Image对象(它位于ListView对象的DataTemplate中):

  <Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" />

我应该如何获得高质量的双三次插值图像? (在屏幕上,此图像的大小小于源PNG,但默认大小调整似乎是使用质量差的“最近邻居”插值执行的。)

由于我想单独依赖数据绑定(每当关联数据项的ImgSource发生变化时,图像内容应该更改),我试图设置一个ImageOpened处理程序并将刚刚加载的图像更改为更好质量一。

不幸的是,下面的代码似乎不起作用(我只是得到空图像):

    async void LoadImage(Image imgControl, string source)
    {
        try
        {
            StorageFile file = await StorageFile.GetFileFromPathAsync(source);

            IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

            InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
            BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);

            enc.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic;
            enc.BitmapTransform.ScaledHeight = Convert.ToUInt32(imgControl.ActualHeight);
            enc.BitmapTransform.ScaledWidth = Convert.ToUInt32(imgControl.ActualWidth);

            await enc.FlushAsync();

            Windows.UI.Xaml.Media.Imaging.BitmapImage bImg = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
            bImg.SetSource(ras);
            imgControl.Source = bImg;
        }
        catch (Exception e)
        {
            return;
        }
    }

    void img_ImageOpened(object sender, RoutedEventArgs e)
    {
        Image imgControl = (Image)sender;
        LoadImage(imgControl, <path to PNG file>);
    }

2 个答案:

答案 0 :(得分:2)

我在WinRT应用程序中遇到了相同的图像质量问题,并尝试使用RenderOptions.BitmapScalingMode,但它在.NET Store for .NET Store中不存在(以及System.Windows.Media命名空间)。所以我尝试了你的第一个解决方案并修复它以便工作。你只是成功的一小步,只需要添加

ras.Seek(0);

允许从头开始读取流。

答案 1 :(得分:-1)

我知道这有点晚了,但对其他人来说这可能是一个有用的参考。不需要这样的处理程序:您只需要设置RenderOptions.BitmapScalingMode附加属性的值。

<Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" RenderOptions.BitmapScalingMode="HighQuality" />
相关问题