nullreferenceexception in PngEncoder.Encode()

Apr 17, 2014 at 5:43 AM
I'm sure I'm doing something stupid here (C# novice).

Help appreciated. This is a Windows Phone Silverlight project.
            static void SaveAsPng( WriteableBitmap bmp, IsolatedStorageFileStream stream )
            {
                    ImageTools.IO.Encoders.AddEncoder<PngEncoder>();
                    var pngEncoder = new ImageTools.IO.Png.PngEncoder();

                var imageStream = new MemoryStream();

                var imageData = new ImageTools.ExtendedImage( bmp.ToImage() );
                      
                pngEncoder.Encode( imageData, imageStream );   <<< HERE

                byte[] binaryData = new byte[imageStream.Length];
                imageStream.Read( binaryData, 0, (int)imageStream.Length );

                stream.Write( binaryData, 0, binaryData.Length );
            }