加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:45:24 所属栏目:Windows 来源:网络整理
导读:给定以下 Image对象(它位于ListView对象的DataTemplate中): Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" / 我怎么能得到一个高质量的双三次插值图像? (在屏幕上,此图像的大小小于源PNG,但默认大小调整似乎是使用质量差的“最近邻居
给定以下 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>);
    }

解决方法

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

ras.Seek(0);

允许从头开始读取流.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读