Kyle
Kyle

Reputation: 22045

WPF - How do I save a PNG without any alpha channel?

I have a BitmapSource. I save it to a png like this:

PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(myBitmapSource);
enc.Save(fs);

How can I save it without any alpha channel?

Upvotes: 5

Views: 1998

Answers (2)

Ray Burns
Ray Burns

Reputation: 62919

Use FormatConvertedBitmap to convert to 24 bits per pixel before encoding it:

var noAlphaSource = new FormatConvertedBitmap
{
  Source = myBitmapSource,
  DestinationFormat = PixelFormats.Rgb24
};

var encoder = new PngBitmapEncoder();
enc.Frames.Add(noAlphaSource);
enc.Save(fs);

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 942040

A 24bpp bitmap doesn't have an alpha channel. Supported by the PNG encoder. Create a WriteableBitmap with PixelFormats.Rgb24.

Upvotes: 1

Related Questions