Share via


How to: Encode and Decode a PNG Image

The following examples show how to decode and encode a Portable Network Graphics (PNG) image using the specific PngBitmapDecoder and PngBitmapEncoder objects. For the complete sample, see PNG Encoder and Decoder Sample.

Example

This example demonstrates how to decode a PNG image using a PngBitmapDecoder from a FileStream.

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

This example demonstrates how to encode a BitmapSource into a PNG image using a PngBitmapEncoder.

int width = 128;
int height = 128;
int stride = width;
array<System::Byte>^ pixels = gcnew array<System::Byte>(width * 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[width * 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);

See Also

Concepts

Imaging Overview