C#获取宽度/高度的图像在网上没有下载整个文件? [英] C# get width/height of image on web without downloading whole file?

查看:131
本文介绍了C#获取宽度/高度的图像在网上没有下载整个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信对于JPG,宽度和高度信息存储在前几个字节的 < a>。

I believe with JPGs, the width and height information is stored within the first few bytes. What's the easiest way to get this information given an absolute URI?

推荐答案

首先,您可以请求图片的前100个字节使用 Range 标头。

First, you can request the first hundred bytes of an image using the Range header.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Set(HttpRequestHeader.UserAgent, "Range: bytes=0-100");

接下来,您需要解码。 unix 文件命令具有通用格式表和关键信息的位置。我建议安装Cygwin,并看看 / usr / share / file / magic

Next, you need to decode. The unix file command has a table of common formats, and the locations of key information. I'd suggest installing Cygwin and taking a look at /usr/share/file/magic.

对于 gif png 您可以轻松地从前32个字节获取图像尺寸。但是,对于JPEGs,@Andrew是正确的,因为你不能可靠地获得这些信息。

For gif's and png's, you can easily get the image dimensions from the first 32 bytes. However, for JPEGs, @Andrew is correct in that you can't reliably get this information. You can figure out if it has a thumbnail, and the size of the thumbnail.

获取实际的jpeg大小,您需要扫描开始框架标记。不幸的是,人们不能可靠地确定这将是提前在哪里,并且缩略图可以推动它几千字节到文件。

The get the actual jpeg size, you need to scan for the start of frame tag. Unfortunately, one can't reliably determine where this is going to be in advance, and a thumbnail could push it several thousand bytes into the file.

我建议使用范围请求获取前32个字节。这将让您确定文件类型。之后,如果是JPEG,则下载整个文件,并使用库来获取大小信息。

I'd recommend using the range request to get the first 32 bytes. This will let you determine the file type. After which, if it's a JPEG, then download the whole file, and use a library to get the size information.

这篇关于C#获取宽度/高度的图像在网上没有下载整个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆