This documentation is archived and is not being maintained.

PixelFormats Class

Represents the collection of supported pixel formats.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

Public NotInheritable Class PixelFormats
You do not need to declare an instance of a static class in order to access its members.
You cannot directly create an instance of this class in XAML.

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

<Page xmlns=""


    <!-- 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\Forest.jpg"/>

    <!-- This FormatConvertedBitmap uses the BitmapImage defined above and
         changes the format to Gray32Float (grayscale). -->
        <FormatConvertedBitmap x:Key="convertFormatImage" 
                               Source="{StaticResource masterImage}" 
                               DestinationFormat="Gray32Float" />



    <!-- Apply the "convertFormatImage" resource defined above to this image.  -->
    <Image Width="200" Source="{StaticResource convertFormatImage}" />
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

Namespace SDKSample

    Class FormatConvertedBitmapExample
        Inherits Page

        Public Sub New()

            '/// Create a BitmapImage and set it's DecodePixelWidth to 200. Use  ///// 
            '/// this BitmapImage as a source for other BitmapSource objects.    ///// 
            Dim myBitmapImage As New BitmapImage()

            ' BitmapSource objects like BitmapImage can only have their properties 
            ' changed within a BeginInit/EndInit block.
            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

            '//////// 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. 
            Dim newFormatedBitmapSource As New FormatConvertedBitmap()

            ' BitmapSource objects like FormatConvertedBitmap can only have their properties 
            ' changed within a BeginInit/EndInit block.

            ' 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

            ' Create Image Element 
            Dim myImage As New Image()
            myImage.Width = 200
            'set image source
            myImage.Source = newFormatedBitmapSource

            ' Add Image to the UI 
            Dim myStackPanel As New StackPanel()
            Me.Content = myStackPanel

        End Sub 'New 
    End Class 'FormatConvertedBitmapExample
End Namespace 'ImagingSnippetGallery


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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0