jltest
jltest

Reputation: 21

C# saving multiple png file from a MemoryStream

I am getting multiple pngs from another process from its standard output as a stream. I want to take this memory stream and save it as multiple png files. I have looked at PngBitmapEncoder/PngBitmapDecoder, but I can't seem to get a multiple page out of it (whenever I create a decoder using PngBitmapDecoder.Create, decoder.Frames.Count is always 1. Here is how I create the decoder:

BitmapDecoder decoder = PngBitmapDecoder.Create(memStream,
                                                BitmapCreateOptions.PreservePixelFormat,
                                                BitmapCacheOption.Default);

Am I doing something wrong?

Upvotes: 2

Views: 2079

Answers (4)

Danilo Gasques
Danilo Gasques

Reputation: 676

Yes, there is such a thing as a multi-page PNG. It's called MNG (Multiple-image Network Graphics). It's almost as old as PNG (Check libpng.org for the format MNG).

And there is a C# library that can help you with that

http://www.codeproject.com/Articles/35289/NET-MNG-Viewer

In the last 4 years a format called APNG (Animated Portable Network Graphics) started being accepted and used by browsers like Firefox. There is a wrapper for C#

https://code.google.com/p/sharpapng/

Saving multiple PNGs using one file only will be much faster than using multiple files.

Upvotes: 0

leonbloy
leonbloy

Reputation: 75936

i am getting multiple pngs from another process from its standard output as a stream

It's not clear what this means. PNG does not support multiple image or pages in one file. Are you receiving several PNG files concatenated as a single stream? If this is the case (that would be rather strange) you don't really need to decode the PNGs, just to split the stream and write each one (blindly) in a different file. A quick and dirty approach (not totally foolproof) is to scan the stream for the PNG signature (8 bytes) to detect the start of a new image.

If you rather want to decode the succesive streams (seems overkill), you can use this pngcs library, instantiating a PngReader for each image; just be sure to call PngReader.ShouldCloseStream(false) so that the stream is not close when each image ends.

Upvotes: 0

SLaks
SLaks

Reputation: 887509

There is no such thing a s multi-page PNG.
A PNG decoder will never return more than one frame.

You need to read each image separately.

Upvotes: 3

Related Questions