假设有以下的图片对象(它在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>);
}