コード例 #1
0
            public void Write(Stream file)
            {
                var encoder = new PngBitmapEncoder();

                encoder.Frames.Add(BitmapFrame.Create(m_bitmap));
                using (var png = new MemoryStream())
                    using (var cwp = new BinaryWriter(file, System.Text.Encoding.ASCII, true))
                    {
                        encoder.Save(png);
                        var header = new byte[0x11];
                        png.Position = 0x10;
                        png.Read(header, 0, header.Length);
                        cwp.Write(0x50445743u); // 'CWDP'
                        cwp.Write(header, 0, header.Length);
                        long idat;
                        using (var bin = new BinMemoryStream(png, ""))
                            idat = PngFormat.FindChunk(bin, "IDAT");
                        if (-1 == idat)
                        {
                            throw new InvalidFormatException("CWP conversion failed");
                        }
                        png.Position = idat;
                        png.Read(header, 0, 8);
                        int chunk_size = BigEndian.ToInt32(header, 0) + 4;
                        cwp.Write(header, 0, 4);
                        for (;;)
                        {
                            CopyChunk(png, file, chunk_size);
                            if (8 != png.Read(header, 0, 8))
                            {
                                throw new InvalidFormatException("CWP conversion failed");
                            }
                            if (Binary.AsciiEqual(header, 4, "IEND"))
                            {
                                cwp.Write((byte)0);
                                break;
                            }
                            chunk_size = BigEndian.ToInt32(header, 0) + 4;
                            cwp.Write(header, 0, 8);
                        }
                    }
            }