Example Download: http://www.lionsky.net/mywebsite/downsoft/list.aspx? Id = 221
TOTHUMBNAILIMAGE.ASPX
<% @ Page language = "c #" codebehind = "TOTHUMBNAILIMAGE.ASPX.CS" src = "TOTHUMBNAILIMAGE.ASPX.CS" autoeventwireup = "false"
Inherits = "exam_c.tothumbnailimage"%>
TOTHUMBNAILIMAGE.ASPX.CS
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.drawing;
Using system.Web;
Using system.Web.SessionState;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Using system.drawing.image;
Namespace Exam_C
{
///
/// TothumbnailImage's summary description.
/// summary>
Public Class TothumbnailImage: System.Web.ui.page
/ *
Create by Lion
2003-05-20 19:00
Copyright (c) 2004 www.lionsky.net. All Rights Reserved.
Web: http://www.lionsky.net;
Email: lion-a@sohu.com
* /
Static hashtable htmimes = new hashtable ();
INTERNAL READONLY STRING ALLOWEXT = ".jpe | .jpeg | .jpg | .png | .tif | .tiff | .bmp";
#Region web form designer generated code
Override protected void oninit (Eventargs E)
#Region htmimes [". JPE"] = "image / jpeg";
HTMIMES ["JPEG"] = "image / jpeg";
HTMIMES ["JPG"] = "image / jpeg";
HTMIMES ["PNG"] = "image / png";
HTMIMES [". TIF"] = "image / TIFF";
HTMIMES [". TIFF"] = "image / TIFF";
HTMIMES [". BMP"] = "image / bmp";
# endRegion // Call generation thumbnail method
TOTHUMBNAILIMAGES ("Lionsky.jpg", "B.GIF", 300);
}
#ndregion
#Region Helper
/// Get all related information of the image codec
/// String param> containing the multi-purpose Internet mail expansion protocol (MIME) type encoding decoder
/// Returns all related information of the image codec returns>
Static ImageCodecInfo getCodecInfo (String MimeType)
ImageCodecInfo [] CODECINFO = ImageCodecInfo.getImageEncoders ();
FOREACH (ImageCodecInfo ICI in CodecInfo)
Ici.mimetype == mimetype) Return ICI;
Return NULL;
/// Detect the validity of the extension
/// File name extension param>
/// Returns true if the extension is valid, otherwise returns false. returns>
Bool CheckValidext (String Sext)
Bool flag = false;
String [] ate = allowext.split ('|');
FOREACH (String FileType in axt)
IF (filetype.tolower () == sext)
Flag = true;
Break;
Return flag;
/// save Picture
/// image object param>
/// Save Path param>
/// The codec parameter of the specified format param>
Void SaveImage (System.drawing.Image Image, String SavePath, ImageCodecInfo ICI)
/ / Set an EncoderParameters object for the original image object
Encoderparameters parameters = new encoderparameters (1);
Parameters.Param [0] = New ENCODERPARETER (Encoder.quality, ((long) 90));
Image.save (SavePath, ICI, Parameters);
Parameters.dispose ();
#Region Methods
/// Generate thumbnails
/// Original image path (relative path) param>
/// The thumbnail path is saved as the original picture path (relative path) param> /// thumbnail width ( Highly generated by the source image ratio) param>
Public void TothumbnailImages (String SourceImagePath, Int thumbnailimagewidth)
String sourceImagePath = sourceImagepath;
String thumbnailimagePath = thumbnailimagepath;
Int thumbnailimagewidth = thumbnailimagewidth;
String sext = sourceImagePath.Substring (SourceImagePath.lastIndexof (".")). TOLOWER ();
IF (SourceImagePath.Tostring () == System.String.empty) throw new nullreferenceException ("SourceImagePath
Is NULL! ");
IF (! Checkvalidext (sext))
Throw new ArgumentException ("The original image file format is incorrect, the supported format has [" allowext "
], "SourceImagePath");
// Create an image object from the original picture
System.drawing.Image Image = system.drawing.image.fromfile (httpcontext.current.server.mappath
SourceImagePath);
INT NUM = ((ThumbnailimageWidth / 4) * 3);
Int width = image.width;
INT height = image.height;
/ / Calculate the proportion of pictures
IF ((Double) Width / (Double) Height)> = 1.3333333F)
Num = (Height * thumbnailimagewidth) / width);
Else
ThumbnailimageWidth = (width * num) / height;
IF ((ThumbnailImageWidth <1) || (Num <1))
Return;
// initialize new instances of Bitmap classes with the specified size and format
Bitmap Bitmap = New Bitmap (thumbnailimageWidth, Num, Pixelformat.Format32bppargb);
/ / Create a new Graphics object from the specified image object
Graphics graphics = graphics.fromimage (bitmap);
/ / Clear the entire drawing and fill in transparent background
Graphics.clear (color.Transparent);
/ / Draw the original picture object in the specified position and press the specified size
Graphics.drawImage (Image, New Rectangle (0, 0, ThumbnailImageWidth, Num);
Image.dispose ();
Try {
/ / Save this original image in a specified format and saved to the specified file with the specified codec parameter
String SavePath = (thumbnailimagePath == NULL? SourceImagePath: thumbnailimagePath);
SaveImage (Bitmap, httpcontext.current.server.mappath (savepath), getCodecInfo ((String) HTMIMES
[sext]));
Catch (System.exception E)
Throw e;
Finally
Bitmap.dispose ();
Graphics.dispose ();
html>