从 URL 图像动态生成缩略图

发布于 2024-12-13 15:19:13 字数 156 浏览 0 评论 0原文

我想知道是否有人有一些代码/有用的链接解释我如何做到这一点。 我很可能会这样使用它:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-12-20 15:19:13

使用通用处理程序文件 (.ashx),而不是使用 .aspx 文件。这意味着您只需要 1 个文件(无标记文件),因此它更整洁、更快。毕竟,您没有生成 Web 表单,因此 .aspx 文件不是您想要的。

您可以使用 System.Drawing.Graphics.DrawImage() 生成缩略图。

像这样简单的事情就可以工作:

Bitmap bmpOut = new Bitmap(width, height);
Bitmap B = new Bitmap(context.Server.MapPath(ImageURL));

Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(B, 0, 0, width, height);


context.Response.ContentType = "image/PNG";
MemoryStream MemStream = new MemoryStream();
B.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);

B.Dispose();

MemStream.WriteTo(HttpContext.Current.Response.OutputStream);

Where width & heightinteger 值,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:

Bitmap bmpOut = new Bitmap(width, height);
Bitmap B = new Bitmap(context.Server.MapPath(ImageURL));

Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(B, 0, 0, width, height);


context.Response.ContentType = "image/PNG";
MemoryStream MemStream = new MemoryStream();
B.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);

B.Dispose();

MemStream.WriteTo(HttpContext.Current.Response.OutputStream);

Where width & height are integer values, and ImageURL is a local URI string

I 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.

很酷不放纵 2024-12-20 15:19:13

你走对了:我曾经做过类似的事情,只使用了 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文