XAML图像质量(插值)在Metro风格应用中的表现

5

假设有以下的图片对象(它在ListView对象的DataTemplate中):

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

我该如何获得高质量的双三次插值图像?(在屏幕上,此图像的大小小于源PNG,但默认调整大小似乎采用了质量较差的“最近邻”插值。)
由于我想仅依赖数据绑定(每当相关数据项的ImgSource更改时,Image内容都应更改),因此我尝试设置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个回答

2
我在WinRT应用程序中遇到了相同的图像质量问题,并尝试使用RenderOptions.BitmapScalingMode解决,但在Windows Store的.NET中它不存在(以及System.Windows.Media命名空间)。因此,我尝试了您的第一个解决方案并修复了它,使其正常工作。您距离成功只有一小步之遥,只需要添加

即可。
ras.Seek(0);

允许从开头读取流。


-1

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

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

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