从 URL 图像动态生成缩略图
我想知道是否有人有一些代码/有用的链接解释我如何做到这一点。 我很可能会这样使用它:
<img src="ShowThumb.aspx?image=http://the_image.jpg" alt="" />
谢谢。
I was wondering if anyone has some code/useful link explaining on how I could do this.
I would most likely use it this way:
<img src="ShowThumb.aspx?image=http://the_image.jpg" alt="" />
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用通用处理程序文件 (
.ashx
),而不是使用.aspx
文件。这意味着您只需要 1 个文件(无标记文件),因此它更整洁、更快。毕竟,您没有生成 Web 表单,因此.aspx
文件不是您想要的。您可以使用
System.Drawing.Graphics.DrawImage()
生成缩略图。像这样简单的事情就可以工作:
Where
width
&height
是integer
值,ImageURL
是本地 URI 字符串我不会使用 HTML/CSS 中的宽度/高度属性来调整图像大小。这意味着您将全尺寸图像发送到客户端,浪费了每个人的带宽和时间!此外,当 HTML 调整它的大小时,它通常会做一些粗制滥造的扭曲工作。
顺便说一句,我曾经像这样生成动态缩略图,但我认为,当每次发出 HTTP 请求时都要调整图像大小时,性能会受到影响。
因此,每当上传主图像时,我都会将缩略图保存到物理位置,并在 HTML 中调用它。
Rather than using an
.aspx
file, use a Generic Handler file (.ashx
). This means you only require the 1 file (no markup file), so its a bit neater and a bit quicker. Afterall, you're not generating a web form, so an.aspx
file isn't what you're after.You can make use of
System.Drawing.Graphics.DrawImage()
to produce a thumbnail.Something as simple as this will work:
Where
width
&height
areinteger
values, andImageURL
is a local URI stringI wouldn't resize your image using width/height attributes in HTML/CSS. This means you are sending the full size image to the client wasting everyones bandwidth and time! Furthermore, when HTML resizes it, it usually does a shoddy distorted job of it.
On a side note, I used to generate on-the-fly thumbnails like this, but decided that the performance was compromised as you are resizing the image everytime a HTTP request is made.
Therefore, whenever the main image was uploaded, I saved the thumbnail to a physical location, and called that in my HTML.
你走对了:我曾经做过类似的事情,只使用了 HTTP-Handler (ashx)。在那里您可以使用 Image 类加载 .jpg 并调整大小并返回它。
不要忘记限制处理程序仅调整您域上的图像大小,否则可能会变得非常危险(容易遭受 DOS 攻击)。
Youre on the right track: I did something like this once, only used an HTTP-Handler (ashx) for that. In there you can use the Image class to load the .jpg and resize and return it.
DON'T FORGET to restict the handler to only resize images on your domain, otherwise it could get quite dangerous (easy DOS-Attacks).