Can't load PNG with PngDecoder

Feb 22, 2014 at 5:31 PM
Edited Feb 22, 2014 at 5:46 PM
I'm using this fork: https://github.com/Taggersoft/ImageTools
It is for Windows Phone 8.
I've compared png part of library in original lib and fork - they are equal.

When I read PNG files with it I get "CRC Error. PNG Image chunk is corrupt!" exception.
My code:
public static Texture2D TextureFromStream(GraphicsDevice graphicsDevice, Stream stream, String dbg_nm)
        {
            byte [] b = new byte[4];
            stream.Read(b, 0, 4);
            stream.Seek(0, SeekOrigin.Begin);
            int stream_type = getStreamType(b, 0);
            Texture2D result = null;

            if (stream_type == 1 || stream_type == 0)
            {
                var image = new ExtendedImage();
                if (stream_type == 0)
                    new JpegDecoderNano().Decode(image, stream);
                else
                {
                    try
                    {
                        new PngDecoder().Decode(image, stream);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.WriteLine("Error reading " + dbg_nm + ":" + ex.Message);
                    }                   
                }

                var Colors = new Color[image.Pixels.Length / 4];
                for (var i = 0; i < image.Pixels.Length / 4; i++)
                {
                    Color unpackedColor = new Color();
                    unpackedColor.R = (byte)(image.Pixels[i * 4 + 0]);
                    unpackedColor.G = (byte)(image.Pixels[i * 4 + 1]);
                    unpackedColor.B = (byte)(image.Pixels[i * 4 + 2]);
                    unpackedColor.A = (byte)(image.Pixels[i * 4 + 3]);
                    Colors[i] = unpackedColor;
                }
                Texture2D texture2D = new Texture2D(graphicsDevice, image.PixelWidth, image.PixelHeight);
                texture2D.SetData(Colors);
                result = texture2D;
            }
            
            return result;
}
This code works for JPEGS, not PNGs (32 bit with alpha).
As I debugged I see that problems begin when parsing png text chunk - it somehow contain japanese symbols.
If I comment it, next I get crc error. Then, if I coment crc check, it tells that there is no end of file. Problem appear with every png file I have (they are made by Photoshop).
Can someone help with this strange problem? I assume everybody use PNGs?

UPDATE:
Just have found that CRC error appears when library tries to read beyond memory stream and gets zeroes. Investigating...
Feb 22, 2014 at 6:06 PM
Yes, problem was with stream size, I created it bigger than file.
Also strange exits were caused by Contract.Requires<ArgumentException> statements. They made app quit even if conditions were ok.
I've removed them and everything works now.
Marked as answer by Tertium on 2/22/2014 at 11:06 AM
Mar 4, 2014 at 6:18 AM
In what way you want to load your png image? Loading png from file or loading from .net graphics? Yes, image stream size will affect image loading as well as your reading and processing systems. You must be sure that whether the image loading tool and pnd decoder is fit for your operating system or not.
Apr 15, 2014 at 4:09 AM
I've followed the suggestion and everything goes well for using this .NET Image Tool now. Thanks!