Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FormatConvertedBitmap Class

Provides pixel format conversion functionality for a BitmapSource. This class cannot be inherited.

Namespace: System.Windows.Media.Imaging
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public sealed class FormatConvertedBitmap : BitmapSource, ISupportInitialize
public final class FormatConvertedBitmap extends BitmapSource implements ISupportInitialize
public final class FormatConvertedBitmap extends BitmapSource implements ISupportInitialize
<FormatConvertedBitmap .../>

FormatConvertedBitmap implements the ISupportInitialize interface to optimize initialization on multiple properties. Property changes can only occur during object initialization. Call BeginInit to signal that initialization has begun and EndInit to signal that initialization has completed. Once initialized, property changes are ignored.

FormatConvertedBitmap objects created using the FormatConvertedBitmap(BitmapSource,PixelFormat,BitmapPalette,Double) constructor are automatically initialized and property changes are ignored.

For format conversions between RGB and CMYK color spaces or between RGB color spaces with different characteristics, the ColorConvertedBitmap class should be used. Conversions between color spaces need a source color profile and destination color profile for gamut mapping to provide a reliable color conversion. The color profiles are provided via the ColorContext parameters of the ColorConvertedBitmap or via the DestinationColorContext and SourceColorContext properties.

Metadata tags related to image data must be updated if an image is saved to a file after a transform is applied.

This example shows how to convert an image to grayscale using FormatConvertedBitmap.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Page.Resources>

    <!-- This resource defines a BitmapImage with a source and a
         DecodePixelWidth of 200. This property is set to the same value 
         as the desired width of the image to save on memory use. This 
         BitmapImage is used as the base for the other BitmapSource resources. -->
    <BitmapImage x:Key="masterImage" DecodePixelWidth="200" 
     UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"/>

    <!-- This FormatConvertedBitmap uses the BitmapImage defined above and
         changes the format to Gray32 (grayscale). -->
    <FormatConvertedBitmap x:Key="convertFormatImage" Source="{StaticResource masterImage}"
     DestinationFormat="Gray32" />
      
  </Page.Resources>
  
  <StackPanel>
    
    <!-- Apply the "convertFormatImage" resource defined above to this image.  -->
    <Image Width="200" Source="{StaticResource convertFormatImage}" />
  </StackPanel>
</Page>

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SDKSample
{
    public partial class FormatConvertedBitmapExample : Page
    {
        public FormatConvertedBitmapExample()
        {

            ///// Create a BitmapImage and set it's DecodePixelWidth to 200. Use  /////
            ///// this BitmapImage as a source for other BitmapSource objects.    /////

            BitmapImage myBitmapImage = new BitmapImage();

            // BitmapSource objects like BitmapImage can only have their properties
            // changed within a BeginInit/EndInit block.
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(@"sampleImages/WaterLilies.jpg",UriKind.Relative);

            // To save significant application memory, set the DecodePixelWidth or  
            // DecodePixelHeight of the BitmapImage value of the image source to the desired 
            // height or width of the rendered image. If you don't do this, the application will 
            // cache the image as though it were rendered as its normal size rather then just 
            // the size that is displayed.
            // Note: In order to preserve aspect ratio, set DecodePixelWidth
            // or DecodePixelHeight but not both.
            myBitmapImage.DecodePixelWidth = 200;
            myBitmapImage.EndInit();

            ////////// Convert the BitmapSource to a new format ////////////
            // Use the BitmapImage created above as the source for a new BitmapSource object
            // which is set to a gray scale format using the FormatConvertedBitmap BitmapSource.                                               
            // Note: New BitmapSource does not cache. It is always pulled when required.

            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();

            // BitmapSource objects like FormatConvertedBitmap can only have their properties
            // changed within a BeginInit/EndInit block.
            newFormatedBitmapSource.BeginInit();

            // Use the BitmapSource object defined above as the source for this new 
            // BitmapSource (chain the BitmapSource objects together).
            newFormatedBitmapSource.Source = myBitmapImage;

            // Set the new format to Gray32Float (grayscale).
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
            newFormatedBitmapSource.EndInit();

            // Create Image Element
            Image myImage = new Image();
            myImage.Width = 200;
            //set image source
            myImage.Source = newFormatedBitmapSource;

            // Add Image to the UI
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Children.Add(myImage);
            this.Content = myStackPanel;
        }
    }
}

More Code

How to: Convert a BitmapSource to an Indexed Pixel Format

This example shows how to convert a BitmapSource to an indexed pixel format.

How to: Chain BitmapSource Objects Together

This example shows how you can apply a variety of effects to an image source by chaining multiple BitmapSource derived objects together.

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Media.ImageSource
             System.Windows.Media.Imaging.BitmapSource
              System.Windows.Media.Imaging.FormatConvertedBitmap
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.