public class Program1
{
static public void Main()
{
var buf = new byte[0xffff];
int width = 0;
int height = 0;
string path = @"F:\csharp\ImgView2\00201.png";
path = @"F:\csharp\ImgView2\00201.png";
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
fs.Read(buf, 0, buf.Length);
// PNG
if (buf.Take(8).SequenceEqual(new byte[]{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,}))
{
var w = buf.Skip(8+8+0).Take(4).Reverse().ToArray();
var h = buf.Skip(8+8+4).Take(4).Reverse().ToArray();
width = BitConverter.ToInt32(w);
height = BitConverter.ToInt32(h);
Console.WriteLine($"PNG: Width:{width} Height:{height}");
}
// JPEG
if (buf.Take(2).SequenceEqual(new byte[] { 0xff, 0xd8, }))
{
var i = buf.Select((x,i) => new { index = i, value = buf.Skip(i).Take(2)})
.Where(x => x.value.SequenceEqual(new byte[] { 0xff, 0xc0,} ))
.Select(x => x.index)
.DefaultIfEmpty(-1)
.First();
if (i >= 0)
{
var w = buf.Skip(i+2+5).Take(2).Reverse().ToArray();
var h = buf.Skip(i+2+3).Take(2).Reverse().ToArray();
width = (int)BitConverter.ToInt16(w);
height = (int)BitConverter.ToInt16(h);
Console.WriteLine($"JPEG: Width:{width} Height:{height}");
}
}
}
}
コメント