Implementation Code for generating thumbnails in asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Drawing;
Using System. IO;
Namespace web Layer 3
{
/// <Summary>
/// Display the thumbnail of the requested image, in the maximum unit of 100 pixels in width
/// </Summary>
Public class imgSmall: IHttpHandler
{
// Folder where the image is located
Static string picturesPath = @ "d: \ wordpictures \";
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "image/jpeg ";
// Obtain the passed img string, such
// Http: // localhost: 5002/imgSmall. ashx? Img1_abacus8.jpg
String img = context. Request. Params ["img"];
String path = picturesPath + img;
// If the file exists, it will be read. Reduce the use of try and catch to improve program performance.
If (File. Exists (path ))
{
// Load the image
Image big = Image. FromFile (path );
// If the file can be obtained, the following code will be executed.
If (big! = Null)
{
// Set the maximum width, which can be modified to generate smaller thumbnails
Int newWidth = 100;
// Generate a bitmap based on the aspect ratio of the image.
Bitmap bitmap = new Bitmap (newWidth, newWidth * big. Height/big. Width );
// Create a picture based on the palette
Graphics g = Graphics. FromImage (bitmap );
Using (g)
{
// Draw a big image to a bitmap defined by yourself
G. DrawImage (big, 0, 0, bitmap. Width, bitmap. Height );
// Directly Save the processed bitmap to the response output stream. The format is jpeg!
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
}
Else
{
// Otherwise, a message indicating that the file does not exist will be sent to the browser.
Context. Response. ContentType = "text/html ";
Context. Response. Write ("the file does not exist ");
// Or send an image that does not exist in the file
// Context. Response. WriteFile ("Change todo to image path ");
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}

This is a general processing program. You only need to send a file name parameter to it to display the image in the browser (you can change it to the serial number id of the image to be sent ), for example, this handler is used in the following GridView control.

 

In the thumbnail field, set its DataImageUrlField to the file name. This field is a field in the database (I like to use Chinese fields for better understanding when I am a beginner), and set its DataImageUrlFormatString to imgSmall. ashx? Img = {0}. In this way, {0} is replaced with the file name field when html code is generated. The actual effect is as follows:

The thumbnail size is very small, only 3040 bytes. You can also find that when the browser requests an image, the requested address is imgSmall. ashx? Img1_abacus8.jpg

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.