This documentation is archived and is not being maintained.

BitmapPalette Class

Defines the available color palette for a supported image type.

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

'Declaration
Public NotInheritable Class BitmapPalette _
	Inherits DispatcherObject
'Usage
Dim instance As BitmapPalette
You cannot directly create an instance of this class in XAML.

Only Tagged Image File Format (TIFF) and Graphics Interchange Format (GIF) image formats support image palettes. However, other image types can be used to define a BitmapPalette. Attempting to define a BitmapPalette for an unsupported bitmap format does not result in an exception; the designated BitmapPalette is simply ignored.

Windows Presentation Foundation (WPF) supports both custom color palettes and predefined palettes defined by the BitmapPalettes class.

The following example demonstrates how to define a custom BitmapPalette and apply it to a new BitmapSource.

Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = CType(width / 8, Integer)
Dim pixels(height * stride) As Byte

' Try creating a new image with a custom palette. 
Dim colors As New List(Of System.Windows.Media.Color)()
colors.Add(System.Windows.Media.Colors.Red)
colors.Add(System.Windows.Media.Colors.Blue)
colors.Add(System.Windows.Media.Colors.Green)
Dim myPalette As New BitmapPalette(colors)

' Creates a new empty image with the pre-defined palette 
Dim image As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride)
Dim stream As New FileStream("empty.tif", FileMode.Create)
Dim encoder As New TiffBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
MessageBox.Show(myPalette.Colors.Count.ToString())
encoder.Save(stream)

The following example demonstrates how to retrieve a BitmapPalette from an image.

' Get the palette from an image 
Dim image2 As New BitmapImage()
image2.BeginInit()
image2.UriSource = New Uri("tulipfarm.tif", UriKind.RelativeOrAbsolute)
image2.EndInit()
Dim myPalette3 As New BitmapPalette(image2, 256)

'Draw the third Image 
Dim myImage2 As New Image()
myImage2.Source = image2
myImage2.Stretch = Stretch.None
myImage2.Margin = New Thickness(20)

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.

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.Media.Imaging.BitmapPalette

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
Show: