The FormatConvertedBitmap is used to apply formatting on images in WPF. The FormatConvertedBitmap.Source property is a BitmapImage that will be used in the formatting process.
This code creates a BitmapImage.
BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(@"C:\Books\Book
WPF\How do I\ImageSample\ImageSample\Flower.JPG"); |
This code snippet creates a FormatConvertedBitmap from the BitmapImage created above.
FormatConvertedBitmap grayBitmapSource = new
FormatConvertedBitmap(); grayBitmapSource.BeginInit(); |
grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
private void GrayScaleButton_Click(object
sender, RoutedEventArgs e) { // Create a BitmapSource BitmapImage bitmap = new
BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"C:\Books\Book
WPF\How do I\ImageSample\ImageSample\Flower.JPG");
bitmap.EndInit(); // Create a FormatConvertedBitmap FormatConvertedBitmap grayBitmapSource = new FormatConvertedBitmap(); // BitmapSource objects like FormatConvertedBitmap can only
have their properties // changed within a BeginInit/EndInit block.
grayBitmapSource.BeginInit();
grayBitmapSource.Source = bitmap; // Key of changing the bitmap format is DesitnationFormat
property of BitmapSource. // It is a type of PixelFormat. FixelFormat has dozens of
options to set // bitmap formatting.
grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
grayBitmapSource.EndInit(); // Create a new Image Image grayImage = new
Image();
grayImage.Width = 300; // Set Image source to new FormatConvertedBitmap
grayImage.Source = grayBitmapSource; // Add Image to Window
LayoutRoot.Children.Add(grayImage); |

Join the conversation! Your thoughts help the community grow.