RikWare
...

Setting FormatConvertedBitmap Source via Binding

Thursday, 29 December 2005 05:53 by admin

Avalon's FormatConvertedBitmap is a convenient way of getting a greyscale version of an image, but it appears to have some trouble with setting the source property via data binding. The following code fails complaining that the Source property has not been set on the FormatConvertedBitmap:

<Image Grid.Column="0" Grid.Row="1" Width="400" Height="200">
<Image.Source>
<FormatConvertedBitmap Source="{Binding Path=ImageSource,
Converter={StaticResource imageFormatConverter}}" DestinationFormat="Gray32" />
</Image.Source>
</Image>

I've tested the databinding on a standard image and it is fine. Similarly the code works correctly if a uri is entered instead of the binding expression.

I've no idea why this is happening, but I have managed to find a work around by using a converter to set the outer image's source instead of embedded XAML. e.g.

<Image Grid.Column="1" Grid.Row="1" Source="{Binding Path=ImageSource, 
Converter={StaticResource imageFormatConverter},
ConverterParameter=Gray32}" />

public class FormatConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
BitmapSource source = new BitmapImage(new Uri(value.ToString()));
return new FormatConvertedBitmap(source,
(PixelFormat)new PixelFormatConverter().ConvertFrom(parameter), null, 0);
}

public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}

I suppose I should fire this at the newsgroup and see if I can find more insight into what's happening.

Categories:   Avalon
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment




biuquote
  • Comment
  • Preview
Loading