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

Tak*_*ato 5 datatemplate microsoft-metro winrt-xaml

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

  <Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" />
Run Code Online (Sandbox Code Playgroud)

我怎么能得到一个高质量的双三次插值图像?(在屏幕上,此图像的大小小于源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>);
    }
Run Code Online (Sandbox Code Playgroud)

小智 -1

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

<Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" RenderOptions.BitmapScalingMode="HighQuality" />
Run Code Online (Sandbox Code Playgroud)