Beispiel #1
0
        public static void showChunks(String file)
        {
            PngReader pngr = FileHelper.CreatePngReader(file);

            pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb!
            pngr.ReadSkippingAllRows();
            Console.Out.WriteLine(pngr.ToString());
            Console.Out.WriteLine(pngr.GetChunksList().ToStringFull());
        }
        public static void testRead(String file)
        {
            // register with factory chunk
            PngChunk.FactoryRegister(PngChunkSERI.ID, typeof(PngChunkSERI));
            // read all file
            PngReader pngr = FileHelper.CreatePngReader(file);

            pngr.ReadSkippingAllRows();
            pngr.End();
            // we assume there can be at most one chunk of this type...
            PngChunk chunk = pngr.GetChunksList().GetById1(PngChunkSERI.ID); // This would work even if not registered, but then PngChunk would be of type PngChunkUNKNOWN

            Console.Out.WriteLine(chunk);
            // the following would fail if we had not register the chunk
            PngChunkSERI chunkprop = (PngChunkSERI)chunk;
            string       name      = chunkprop.GetObj().name;
            int          age       = chunkprop.GetObj().age;

            Console.Out.WriteLine("Done. Name: " + name + " age=" + age);
        }
Beispiel #3
0
        public static ResourceDictionary LoadFromPng(string FileName)
        {
            // read all file
            PngReader pngr = FileHelper.CreatePngReader(FileName);

            pngr.ReadSkippingAllRows();
            pngr.End();
            // we assume there can be at most one chunk of this type...
            PngChunk chunk = pngr.GetChunksList().GetById1(PngChunkSKIN.ID); // This would work even if not registered, but then PngChunk would be of type PngChunkUNKNOWN

            if (chunk != null)
            {
                // the following would fail if we had not register the chunk
                PngChunkSKIN  chunkprop = (PngChunkSKIN)chunk;
                ParserContext pc        = new ParserContext();
                pc.XamlTypeMapper = XamlTypeMapper.DefaultMapper;
                //  pc.XmlSpace

                //MimeObjectFactory s;

                var rd1 = (ResourceDictionary)XamlReader.Parse(chunkprop.Content);

                // Application.Current.Resources.MergedDictionaries.Add(rd1);

                //  var rd2 = (ResourceDictionary)XamlReader.Parse(chunkprop.Content);

                ////  Application.Current.Resources.MergedDictionaries.Add(rd2);

                //  if (rd1 == rd2) {
                //  }

                return(rd1);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Writes a new file with several text chunks, reads it back and compares
        /// </summary>

        public static void test()
        {
            Dictionary <string, string> texts = new Dictionary <String, String>();

            texts.Add("key1", "val");
            texts.Add("empty1", "");
            texts.Add("unicode1", "Hernán");
            texts.Add("zero1", "Hola\0chau");
            texts.Add("key2", "val");
            texts.Add("empty2", "");
            texts.Add("unicode2", "Hernán");
            texts.Add("zero2", "Hola\0chau");
            texts.Add("key3", "val");
            texts.Add("empty3", "");
            texts.Add("unicode3", "Hernán");
            texts.Add("zero3", "Hola\0chau");
            texts.Add("nolatin1", "Hernán\u1230");
            String    suffix = "text";
            PngWriter png    = TestsHelper.prepareFileTmp(suffix);

            png.GetMetadata().SetText("key1", texts["key1"], false, false);
            png.GetMetadata().SetText("key2", texts["key2"], true, false);
            png.GetMetadata().SetText("key3", texts["key3"], true, true);
            png.GetMetadata().SetText("empty1", texts["empty1"], false, false);
            png.GetMetadata().SetText("empty2", texts["empty2"], true, false);
            png.GetMetadata().SetText("empty3", texts["empty3"], true, true);
            png.GetMetadata().SetText("unicode1", texts["unicode1"], false, false);
            png.GetMetadata().SetText("unicode2", texts["unicode2"], true, false);
            png.GetMetadata().SetText("unicode3", texts["unicode3"], true, true);
            png.GetMetadata().SetText("nolatin1", texts["nolatin1"], false, false);
            png.GetMetadata().SetText("zero1", texts["zero1"], false, false);
            png.GetMetadata().SetText("zero2", texts["zero2"], true, false);
            png.GetMetadata().SetText("zero3", texts["zero3"], true, true);
            TestsHelper.endFileTmp(png);
            PngReader pngr = TestsHelper.getReaderTmp(suffix);

            pngr.ReadSkippingAllRows();
            int ok = 0;

            foreach (PngChunk c in pngr.GetChunksList().GetChunks())
            {
                if (!ChunkHelper.IsText(c))
                {
                    continue;
                }
                ok++;
                PngChunkTextVar ct  = (PngChunkTextVar)c;
                String          key = ct.GetKey();
                String          val = ct.GetVal();
                Console.WriteLine(c.Id + " chunk. Key:" + key + " val='" + val + "'");
                if (!val.Equals(texts[key]))
                {
                    Console.WriteLine("ERROR: expected '" + texts[key] + "' got '" + val
                                      + "' key=" + key + " id=" + c.Id);
                }
            }
            if (ok != texts.Keys.Count)
            {
                throw new Exception("number of text chunks does not coincide");
            }
            Console.WriteLine("done");
        }