给定以下 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 但它在 Windows 应用商店的 .NET 中不存在(以及 System.Windows.Media 命名空间)。所以我尝试了你的第一个解决方案并修复了它,使其有效。您距离成功仅一步之遥,只需添加
ras.Seek(0);
允许从头开始读取流。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)