Tuesday, September 29, 2015

Easier way to convert BitmapImage to Grayscale in WPF

By adding the xmlns namespace xmlns:extra="http://schemas.microsoft.com/netfx/2007/xaml/presentation" I was able to use the FormatConvertedBitmap class.

One way to use it is to define the Bitmap image as a StaticResource:
         
xmlns:extra="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
<Grid x:Name="LayoutRoot" Width="304" Height="360">
  <Grid.Resources>

   <BitmapImage x:Key="MyPhoto" UriSource="Images/myPhoto.png"/>    <extra:FormatConvertedBitmap x:Key="convertedImage" Source="{StaticResource MyPhoto}" DestinationFormat="Gray32Float" />


  </Grid.Resources>

  <Border x:Name="ImageBorder" ClipToBounds="True">


   <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{StaticResource convertedImage}" Margin="-25" ClipToBounds="True" />


  </Border>
</Grid>

Another way I was able to use it - was to put the Binding (from the control's view model) into the actual definition - and still keep the source of the Image as the StaticResource.

<extra:FormatConvertedBitmap x:Key="convertedImage" Source="{Binding ImageSource}" DestinationFormat="Gray32Float" />


More information here.

No comments:

Post a Comment