Export (0) Print
Expand All

How to: Encode and Decode a WDP Image

The following examples show how to decode and encode a Microsoft Windows Media Photo image using the specific WmpBitmapDecoder and WmpBitmapEncoder objects.

This example demonstrates how to decode a Windows Media Photo image using a WmpBitmapDecoder from a Uri.


// Open a Stream and decode a WDP image
Stream^ imageStreamSource = gcnew FileStream("tulipfarm.wdp", FileMode::Open, FileAccess::Read, FileShare::Read);
WmpBitmapDecoder^ decoder = gcnew WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource = decoder->Frames[0];

// Draw the Image
Image^ myImage = gcnew Image();
myImage->Source = bitmapSource;
myImage->Stretch = Stretch::None;
myImage->Margin = System::Windows::Thickness(20);

This example demonstrates how to encode a BitmapSource into a Windows Media Photo image using a WmpBitmapEncoder.

               int width = 128;
               int height = width;
               int stride = width / 8;
               array<System::Byte>^ pixels = gcnew array<System::Byte>(height * stride);

               // Define the image palette
               BitmapPalette^ myPalette = BitmapPalettes::WebPalette;

               // Creates a new empty image with the pre-defined palette

               BitmapSource^ image = BitmapSource::Create(
                  width,
                  height,
                  96,
                  96,
                  PixelFormats::Indexed1,
                  myPalette,
                  pixels,
                  stride);

               FileStream^ stream = gcnew FileStream("new.wdp", FileMode::Create);
               WmpBitmapEncoder^ encoder = gcnew WmpBitmapEncoder();
               TextBlock^ myTextBlock = gcnew TextBlock();
               myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
               encoder->Frames->Add(BitmapFrame::Create(image));
               encoder->Save(stream);
Show:
© 2014 Microsoft