简体   繁体   English

在ASP.NET中显示缩略图的最佳方法是什么?

[英]What's the best way of displaying thumbnails in ASP.NET?

I'm creating a regular gallery in ASP.NET but I have little experiences with creation of thumbnails. 我正在ASP.NET中创建一个常规库,但我没有创建缩略图的经验。 I know the algorithms and the GetThumbnailImage method, but my problem is somewhere else - I'm currently displaying the images (just resized) using the ImageButton control. 我知道算法和GetThumbnailImage方法,但我的问题是在其他地方 - 我正在使用ImageButton控件显示图像(刚刚调整大小)。 And that's the point - I have no idea how to hook up the "thumbnailed" image to the ImageUrl property. 这就是重点 - 我不知道如何将“缩略图”图像连接到ImageUrl属性。 Is it even possible and if yes, how? 甚至是可能的,如果是的话,怎么样? Or should I use some other control instead? 或者我应该使用其他控件吗? Thanks for any suggestions! 谢谢你的任何建议!

It sounds like you need to set up an HttpHandler, which would create the resized image and probably cache it to disk as well, to save having to recreate the thumbnail on each request. 听起来你需要设置一个HttpHandler,它可以创建调整大小的图像,也可以将它缓存到磁盘上,以节省在每个请求上重新创建缩略图的麻烦。

So, for example: 所以,例如:

<asp:ImageButton ID="ImageButton1" ImageUrl="~/ImageHandler.ashx?ImageId=123" runat="server />

You would then have a handler: 然后你会有一个处理程序:

namespace MyProject
{
    public class ImageHandler : IHttpHandler
    {
        public virtual void ProcessRequest(HttpContext context)
        {
            // 1. Get querystring parameter
            // 2. Check if resized image is in cache
            // 3. If not, create it and cache to disk
            // 5. Send the image

            // Example Below
            // -------------

            // Get ID from querystring
            string id = context.Request.QueryString.Get("ImageId");

            // Construct path to cached thumbnail file
            string path = context.Server.MapPath("~/ImageCache/" + id + ".jpg");

            // Create the file if it doesn't exist already
            if (!File.Exists(path))
                CreateThumbnailImage(id);

            // Set content-type, content-length, etc headers

            // Send the file
            Response.TransmitFile(path);
        }

        public virtual bool IsReusable
        {
            get { return true; }
        }
    }
}

You'd also need to set this up in web.config 您还需要在web.config中进行设置

<system.web>
    <httpHandlers>
        <add verb="*" path="ImageHandler.ashx" type="MyProject.ImageHandler, MyProject"/>
    </httpHandlers>
</system.web>

This should be enough to get you started. 这应该足以让你入门。 You'll need to modify the ProcessRequest method to create the thumbnail, but you mentioned having taken care of this already. 您需要修改ProcessRequest方法来创建缩略图,但您提到已经处理过这个问题。 You'll also need to make sure that you set the headers correctly when transmitting the file to the browser. 您还需要确保在将文件传输到浏览器时正确设置标头。

You can create a HttpHandler which handles image requests and returns thumbnails (or does whatever you need on the images). 您可以创建一个HttpHandler来处理图像请求并返回缩略图(或者对图像执行任何操作)。

Whenever you do graphics stuff in ASP.NET, keep in mind that almost all of System.Drawing is a wrapper for GDI+ and thetrefore holds references to unmanaged memory which needs to be disposed properly (use the using statement). 无论何时在ASP.NET中执行图形处理,请记住,几乎所有的System.Drawing都是GDI +的包装器,并且保留了对非托管内存的引用,这些内存需要正确处理(使用using语句)。 This holds true even for simple classes like StringFormat etc. 即使对于StringFormat等简单类也是如此。

Http Handler is the way to go. Http Handler是要走的路。

Another note on performance: manipulating images is expensive relative to disk space, both from a memory and cpu standpoint. 关于性能的另一个注意事项:从内存和CPU的角度来看,操作映像相对于磁盘空间来说是昂贵的。 Therefore generating the thumbnail from a full image is is something you only want to do once for each full image. 因此,从完整图像生成缩略图是您只想为每个完整图像执行一次。 The best time to do it is probably at the time where the image is uploaded, especially if you will be showing a number of these on the same page. 这样做的最佳时间可能是在上传图像时,特别是如果您要在同一页面上显示其中的一些图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET 上检测 JSON 请求的最佳方法是什么 - What's the best way to detect a JSON request on ASP.NET 在ASP.NET中检查用户浏览器的最佳方法是什么? - What is the best way to check the user's browser in ASP.NET? 在 ASP.NET 中显示消息框或确认框的最佳方式 - Best way of displaying a Message box or Confirmation box in ASP.NET 缩略图asp.net - Thumbnails asp.net 在ASP.Net中处理web.config文件版本的最佳方法是什么? - What's the best way to handle web.config file versions in ASP.Net? 在绑定到DataTable的ASP.NET GridView中舍入数字的最佳方法是什么? - What's the best way of rounding numbers in an ASP.NET GridView bounded to a DataTable? 更改架构后更新我的asp.net应用程序/数据库的最佳方法是什么? - What's the best way to update my asp.net application / database after changing the schema? asp.net Core/OnPostAsync:防止记录被创建两次的最佳方法是什么? - asp.net Core/OnPostAsync: what's the best way to prevent a record from being created twice? 为ASP.NET MVC Intranet应用程序实施Windows身份验证的最佳方法是什么? - What's the best way to implement Windows Authentication for an ASP.NET MVC intranet application? 什么是处理异常以及如何在asp.net中处理它们的最佳方法 - What's the best way to handle the exceptions and how to deal with them in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM