void dIHDR(PNGChunk c) { IHDRChunk ic = new IHDRChunk(c); ind(); wo("Width: " + ic.Width); wo("Height: " + ic.Height); wo("BitDepth: " + ic.BitDepth); wo("ColorType: " + ic.ColorType + " " + decodeColorType(ic)); oud(); }
string decodeColorType(IHDRChunk ic) { List<string> ans = new List<string>(); if (ic.HasPalette) ans.Add("palette"); if (ic.HasColor) ans.Add("color"); if (ic.HasAlpha) ans.Add("alpha"); return String.Join(",", ans.ToArray()); }
public static Image ImageFromFile(string fileName) { bool IsPNG = true; List <PNGChunk> chunks = null; try { chunks = ReadPNG(fileName); } catch { // not a PNG file - let .Net handle it IsPNG = false; } if (!IsPNG || chunks[0].Signature != "CgBI") // assume regular PNG { Image ti = Image.FromFile(fileName); Image ans = CopyImage(ti); ti.Dispose(); // make sure we don't lock the file return(ans); } else // build a new PNG image { IHDRChunk hdrChunk = null; // fix IDAT chunks foreach (PNGChunk c in chunks) { if (c.Signature == "IDAT") { byte[] orig = c.Data; byte[] newd = new byte[orig.Length + 6]; newd[0] = 0x78; // 0x38; newd[1] = 0x9c; // 0x8d; orig.CopyTo(newd, 2); c.Data = newd; } else if (c.Signature == "IHDR") { hdrChunk = new IHDRChunk(c); } } MemoryStream stdPNG = new MemoryStream(); BinaryWriter bw = new BinaryWriter(stdPNG); // delete chunk 0 - it was the Apple specific chunk chunks.RemoveAt(0); WritePNG(chunks.ToArray(), bw); stdPNG.Seek(0, SeekOrigin.Begin); Bitmap newi = new Bitmap(stdPNG); bw.Close(); stdPNG.Close(); // Swap the red & blue if color & 8 bpp if (hdrChunk.HasColor && hdrChunk.BitDepth == 8) { SwapRandB(newi, hdrChunk.HasAlpha); } return(newi); } }
public static Image ImageFromFile(string fileName) { bool IsPNG = true; List<PNGChunk> chunks = null; try { chunks = ReadPNG(fileName); } catch { // not a PNG file - let .Net handle it IsPNG = false; } if (!IsPNG || chunks[0].Signature != "CgBI") { // assume regular PNG Image ti = Image.FromFile(fileName); Image ans = CopyImage(ti); ti.Dispose(); // make sure we don't lock the file return ans; } else { // build a new PNG image IHDRChunk hdrChunk = null; // fix IDAT chunks foreach (PNGChunk c in chunks) { if (c.Signature == "IDAT") { byte[] orig = c.Data; byte[] newd = new byte[orig.Length + 6]; newd[0] = 0x78; // 0x38; newd[1] = 0x9c; // 0x8d; orig.CopyTo(newd, 2); c.Data = newd; } else if (c.Signature == "IHDR") { hdrChunk = new IHDRChunk(c); } } MemoryStream stdPNG = new MemoryStream(); BinaryWriter bw = new BinaryWriter(stdPNG); // delete chunk 0 - it was the Apple specific chunk chunks.RemoveAt(0); WritePNG(chunks.ToArray(), bw); stdPNG.Seek(0, SeekOrigin.Begin); Bitmap newi = new Bitmap(stdPNG); bw.Close(); stdPNG.Close(); // Swap the red & blue if color & 8 bpp if (hdrChunk.HasColor && hdrChunk.BitDepth == 8) { SwapRandB(newi, hdrChunk.HasAlpha); } return newi; } }