Comment : encoder et décoder une image PNG
Les exemples suivants montrent comment décoder et encoder une image PNG (Portable Network Graphics) à l’aide des objets et PngBitmapEncoder des objets spécifiquesPngBitmapDecoder.
Décoder une image PNG
Cet exemple montre comment décoder une image PNG à l’aide d’un PngBitmapDecoderFileStream.
// Open a Stream and decode a PNG image
Stream^ imageStreamSource = gcnew FileStream("smiley.png", FileMode::Open, FileAccess::Read, FileShare::Read);
PngBitmapDecoder^ decoder = gcnew PngBitmapDecoder(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 PNG image
Stream imageStreamSource = new FileStream("smiley.png", FileMode.Open, FileAccess.Read, FileShare.Read);
PngBitmapDecoder decoder = new PngBitmapDecoder(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 PNG image
Dim imageStreamSource As New FileStream("smiley.png", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim decoder As New PngBitmapDecoder(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 PNG
Cet exemple montre comment encoder un BitmapSource fichier dans une image PNG à l’aide d’un PngBitmapEncoder.
int width = 128;
int height = 128;
int stride = width;
array<System::Byte>^ pixels = gcnew array<System::Byte>(height * stride);
// Define the image palette
BitmapPalette^ myPalette = BitmapPalettes::Halftone256;
// Creates a new empty image with the pre-defined palette
BitmapSource^ image = BitmapSource::Create(width,
height,
96,
96,
PixelFormats::Indexed8,
myPalette,
pixels,
stride);
FileStream^ stream = gcnew FileStream("new.png", FileMode::Create);
PngBitmapEncoder^ encoder = gcnew PngBitmapEncoder();
TextBlock^ myTextBlock = gcnew TextBlock();
myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
encoder->Interlace = PngInterlaceOption::On;
encoder->Frames->Add(BitmapFrame::Create(image));
encoder->Save(stream);
int width = 128;
int height = 128;
int stride = width;
byte[] pixels = new byte[height * stride];
// Define the image palette
BitmapPalette myPalette = BitmapPalettes.Halftone256;
// Creates a new empty image with the pre-defined palette
BitmapSource image = BitmapSource.Create(
width,
height,
96,
96,
PixelFormats.Indexed8,
myPalette,
pixels,
stride);
FileStream stream = new FileStream("new.png", FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Interlace = PngInterlaceOption.On;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
Dim width As Integer = 128
Dim height As Integer = 128
Dim stride As Integer = width
Dim pixels(height * stride) As Byte
' Define the image palette
Dim myPalette As BitmapPalette = BitmapPalettes.Halftone256
' 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.Indexed8, myPalette, pixels, stride)
Dim stream As New FileStream("new.png", FileMode.Create)
Dim encoder As New PngBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Interlace = PngInterlaceOption.On
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)
Voir aussi
Collaborer avec nous sur GitHub
La source de ce contenu se trouve sur GitHub, où vous pouvez également créer et examiner les problèmes et les demandes de tirage. Pour plus d’informations, consultez notre guide du contributeur.
.NET Desktop feedback