Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was looking for something similar a few months earlier. I wanted to read the type, version, height and width of a GIF image but couldn’t find anything useful online.</p> <p>Fortunately in case of GIF, all the required information was in the first 10 bytes:</p> <pre><code>Type: Bytes 0-2 Version: Bytes 3-5 Height: Bytes 6-7 Width: Bytes 8-9 </code></pre> <p>PNG are slightly more complex (width and height are 4-bytes each):</p> <pre><code>Width: Bytes 16-19 Height: Bytes 20-23 </code></pre> <p>As mentioned above, <a href="http://www.wotsit.org/" rel="nofollow noreferrer">wotsit</a> is a good site for detailed specs on image and data formats though the PNG specs at <a href="http://www.libpng.org/pub/png/spec/1.1/PNG-Contents.html" rel="nofollow noreferrer">pnglib</a> are much more detailed. However, I think the Wikipedia entry on <a href="https://en.wikipedia.org/wiki/Portable_Network_Graphics" rel="nofollow noreferrer">PNG</a> and <a href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format" rel="nofollow noreferrer">GIF</a> formats is the best place to start.</p> <p>Here’s my original code for checking GIFs, I have also slapped together something for PNGs:</p> <pre class="lang-cs prettyprint-override"><code>using System; using System.IO; using System.Text; public class ImageSizeTest { public static void Main() { byte[] bytes = new byte[10]; string gifFile = @"D:\Personal\Images&amp;Pics\iProduct.gif"; using (FileStream fs = File.OpenRead(gifFile)) { fs.Read(bytes, 0, 10); // type (3 bytes), version (3 bytes), width (2 bytes), height (2 bytes) } displayGifInfo(bytes); string pngFile = @"D:\Personal\Images&amp;Pics\WaveletsGamma.png"; using (FileStream fs = File.OpenRead(pngFile)) { fs.Seek(16, SeekOrigin.Begin); // jump to the 16th byte where width and height information is stored fs.Read(bytes, 0, 8); // width (4 bytes), height (4 bytes) } displayPngInfo(bytes); } public static void displayGifInfo(byte[] bytes) { string type = Encoding.ASCII.GetString(bytes, 0, 3); string version = Encoding.ASCII.GetString(bytes, 3, 3); int width = bytes[6] | bytes[7] &lt;&lt; 8; // byte 6 and 7 contain the width but in network byte order so byte 7 has to be left-shifted 8 places and bit-masked to byte 6 int height = bytes[8] | bytes[9] &lt;&lt; 8; // same for height Console.WriteLine("GIF\nType: {0}\nVersion: {1}\nWidth: {2}\nHeight: {3}\n", type, version, width, height); } public static void displayPngInfo(byte[] bytes) { int width = 0, height = 0; for (int i = 0; i &lt;= 3; i++) { width = bytes[i] | width &lt;&lt; 8; height = bytes[i + 4] | height &lt;&lt; 8; } Console.WriteLine("PNG\nWidth: {0}\nHeight: {1}\n", width, height); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload