Reputation: 21
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
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
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#
Saving multiple PNGs using one file only will be much faster than using multiple files.
Upvotes: 0
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
Reputation: 28980
You have sample here on msdn
http://msdn.microsoft.com/fr-fr/library/system.windows.media.imaging.bitmapdecoder.aspx
Upvotes: 0
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