1 out of 1 rated this helpful - Rate this topic

How to: Encode and Decode a BMP Image

The following examples show how to decode and encode a bitmap (BMP) image using the specific BmpBitmapDecoder and BmpBitmapEncoder objects.

This example demonstrates how to decode a BMP image using a BmpBitmapDecoder from a Uri.

' Open a Uri and decode a BMP image 
Dim myUri As New Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute)
Dim decoder2 As New BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource2 As BitmapSource = decoder2.Frames(0)

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

This example demonstrates how to encode a BitmapSource into a BMP image using a BmpBitmapEncoder.

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)()
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("new.bmp", FileMode.Create)
Dim encoder As New BmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.