Given the following image object (it is in the DataTemplate of the ListView object):
<Image Source="{Binding ImgSource}" ImageOpened="img_ImageOpened" />
How should I get a high-quality bicubic interpolated image? (on screen, the size of this image is smaller than the original PNG, but the default resizing is apparently done using poor-quality "nearest neighbor" interpolation).
Since I would like to rely only on data binding (whenever the ImgSource of the associated data item changes, the contents of the image should change), I tried to install the ImageOpened handler and change the image just loaded for the better - quality.
Unfortunately, the code below seems to be inoperable (I just get blank 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>);
}