Metro风格的应用程序中的XAML图像质量(插值) [英] XAML Image Quality (interpolation) in a Metro-Style App

查看:53
本文介绍了Metro风格的应用程序中的XAML图像质量(插值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下图像对象(位于ListView对象的DataTemplate中):

Given the following Image object (it's in the DataTemplate of a ListView object):

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

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

how am I supposed to get an high-quality bicubic-interpolated image? (on screen, the size of this Image is smaller than the source PNG, but the default resizing appears to be performed with the poor-quality "nearest neighbor" interpolation).

由于我只想依靠数据绑定(每当关联数据项的ImgSource更改时,Image内容都应该更改),所以我尝试设置ImageOpened处理程序并将刚加载的图像更改为更好的图像品质的一个.

Since I would like to rely on data binding alone (whenever the ImgSource of the associated data item changes, the Image content should change), I've tried to set an ImageOpened handler and change the just-loaded image to a better-quality one.

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

Unfortunately, the code below seems not to work (I just get empty images):

    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>);
    }

推荐答案

我在WinRT应用程序中遇到了相同的图像质量问题,并尝试使用RenderOptions.BitmapScalingMode,但它不存在(以及System.Windows.Media命名空间与以及适用于Windows应用商店的.NET).因此,我尝试了您的第一个解决方案,并对其进行了修复,使其可以工作.您是成功的一小步,只需添加

I faced the same image quality problem in my WinRT application, and tried to use RenderOptions.BitmapScalingMode but it is not present (and System.Windows.Media namespace as well) in .NET for Windows Store. So I tried your first solution and fixed it so that works. You were one small step from success, only need to add

ras.Seek(0);

允许从头开始读取流.

这篇关于Metro风格的应用程序中的XAML图像质量(插值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆