일부 (모두?) 이미지 형식에는 파일 헤더의 너비 및 높이 속성이 포함됩니다. 헤더를 읽고 자신을 파싱 할 수있을만큼 충분한 바이트를 요청할 수 있습니다. 나는이는 것이다 가정
wReq.AddRange(0, 50);
: 당신은 첫 번째 50 바이트를 요청합니다 웹 요청에 범위 헤더를 추가 할 수있는 이미지 파일로 (50 단지 예입니다, 당신은 아마 덜 필요할 것) 작업중인 형식에이 데이터가 포함되어있는 경우 작동합니다.
편집 : 전에 AddRange
방법을 오해 한 것 같습니다. 이제 해결되었습니다. 나는 또한 앞뒤로 가서 this documentation을 사용하여 png의 너비와 높이를 얻음으로써 그것을 테스트했다.
static void Main(string[] args)
{
string imageUrl = "http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";
byte[] pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(imageUrl);
wReq.AddRange(0, 30);
WebResponse wRes = wReq.GetResponse();
byte[] buffer = new byte[30];
int width = 0;
int height = 0;
using (Stream stream = wRes.GetResponseStream())
{
stream.Read(buffer, 0, 30);
}
// Check for Png
// 8 byte - Signature
// 4 byte - Chunk length
// 4 byte - Chunk type - IDHR (Image Header)
// 4 byte - Width
// 4 byte - Height
// Other stuff we don't care about
if (buffer.Take(8).SequenceEqual(pngSignature))
{
var idhr = buffer.Skip(12);
width = BitConverter.ToInt32(idhr.Skip(4).Take(4).Reverse().ToArray(), 0);
height = BitConverter.ToInt32(idhr.Skip(8).Take(4).Reverse().ToArray(), 0);
}
// Check for Jpg
//else if (buffer.Take(?).SequenceEqual(jpgSignature))
//{
// // Do Jpg stuff
//}
// Check for Gif
//else if (etc...
Console.WriteLine("Width: " + width);
Console.WriteLine("Height: " + height);
Console.ReadKey();
}
jgp 및 gif 서명을 찾는 방법 ... – trocphunc
[누군가 어딘가에 있습니다] (http://stackoverflow.com/questions/4620923/get-dimensions-of-an-image-on-the-web-avoid-memory -hog/4621271 # 4621271)는 jpg 및 gif를 식별하는 방법을 보여줍니다. – Chris