Partager via


Comment : encoder et décoder une image WDP

Les exemples suivants montrent comment décoder et encoder une image photo Microsoft Windows Media à l’aide des objets et WmpBitmapEncoder des objets spécifiquesWmpBitmapDecoder.

Décoder une image photo Windows Media

Cet exemple montre comment décoder une image Photo Windows Media à l’aide d’un WmpBitmapDecoderUri.


// 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);

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

// Draw the Image
Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);
' Open a Stream and decode a WDP image
Dim imageStreamSource As New FileStream("tulipfarm.wdp", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim decoder As New WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource As BitmapSource = decoder.Frames(0)

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

Encoder une image photo Windows Media

Cet exemple montre comment encoder une BitmapSource image photo Windows Media à l’aide d’un 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);

int width = 128;
int height = width;
int stride = width / 8;
byte[] pixels = new 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 = new FileStream("new.wdp", FileMode.Create);
WmpBitmapEncoder encoder = new WmpBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = CType(width / 8, Integer)
Dim pixels(height * stride) As Byte

' Define the image palette
Dim myPalette As BitmapPalette = BitmapPalettes.WebPalette

' 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.wdp", FileMode.Create)
Dim encoder As New WmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)

Voir aussi