ASP. NET2.0 traverses all images in the folder
Author: qingyueer
Home page:Http://blog.csdn.net/21aspnet/Time: 2007.4.4
1. The following directory contains several images (all of which are big images)
2. display on the page
3. Code
Background code
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Runtime. Remoting;
Using System. Runtime. Remoting. Lifetime;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
DirectoryInfo imagesfile = new DirectoryInfo (Server. MapPath ("./images "));
Datalist1.datasource = imagesfile. getfiles ("*. jpg ");
Datalist1.databind ();
}
}
Front-end code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> traverse all the pictures under the folder http://blog.csdn.net/21aspnet clear moon </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
& Nbsp; <asp: datalist id = "datalist1" runat = "server" repeatcolumns = "3">
<Itemtemplate>
<Asp: Image id = "image1" runat = "server" width = "120" imageurl = '<% # "images/" + eval ("name ") %> '/>
</Itemtemplate>
</ASP: datalist> </div>
</Form>
</Body>
</Html>
Note: If you want to generate a real thumbnail, it must be generated on another page.
Imageurl = '<% # "AAA. aspx: Obtain the generated result. Refer to the following code:
Example:Simpleimagehandler. ashx
<% @ WebHandler Language = "C #" Class = "SimpleImageHandler" %>
Using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. IO;
Using System. Web;
Using System. Web. Caching;
Public class SimpleImageHandler: IHttpHandler {
Public void ProcessRequest (HttpContext context)
{
// Load image.
Bitmap bmap = new Bitmap (@ "C:/Temp/TEST. JPG ");
// You cocould do something here, e.g. add some texts to image.
// Send back image.
Context. Response. ContentType = "image/jpeg ";
Context. Response. BufferOutput = false;
Bmap. Save (context. Response. OutputStream, ImageFormat. Jpeg );
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}