Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Drawing;
Using System.IO;
Namespace Web three layer
{
<summary>
Displays the thumbnail of the requested picture, with a width of 100 pixels as the largest unit
</summary>
public class Imgsmall:ihttphandler
{
folder where pictures are located
static string Picturespath = @ "D:\wordpictures\";
public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Image/jpeg";
Gets the IMG string passed over, such as
Http://localhost:5002/imgSmall.ashx?img=abacus8.jpg this
string img = context. Request.params["IMG"];
String path = Picturespath + img;
If the file exists to read, reduce the use of Try,catch, improve program performance
if (file.exists (path))
{
Load this picture
Image big = image.fromfile (path);
If you can get a file, the following code is executed
if (Big!= null)
{
Set the maximum width and can be modified to produce smaller thumbnails
int newwidth = 100;
Generate a bitmap based on the aspect ratio of the picture
Bitmap Bitmap = new Bitmap (newwidth, Newwidth * big. Height/big. Width);
To create a picture from a storyboard
Graphics g = graphics.fromimage (bitmap);
Using (g)
{
Draw big picture into your own little map of your definition bitmap
G.drawimage (big, 0, 0, bitmap.) Width, Bitmap. Height);
Directly saves the processed bitmap to the response output stream in the form of jpeg!
Bitmap. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
Else
{
Otherwise, send a file that does not exist to the browser
Context. Response.ContentType = "text/html";
Context. Response.Write ("File does not exist");
Or send a picture of a file that doesn't exist
Context. Response.WriteFile ("Todo here modified as the path to the picture");
}
}
public bool IsReusable
{
Get
{
return false;
}
}
}
}
This is a generic handler that only needs to send a parameter to the file name to display the picture on the browser (you can modify it to send a picture's ordinal id), such as the one used in the GridView control below
The thumbnail field sets its Dataimageurlfield to the file name, which is a field in the database (I like to use the Chinese field when I am a beginner, I can understand it better), and then set its dataimageurlformatstring to: Imgsmall.ashx?img={0} so that when the HTML code is generated, the {0} is replaced with the contents of the File Name field, the actual effect is as follows:
The thumbnail is very small, only 3040 bytes, reader can also find that when the browser requests a picture, the requested address is imgsmall.ashx?img=abacus8.jpg