Use C # To easily implement thumbnails in DOTNET

Source: Internet
Author: User
Tags dotnet

Previously, third-party components must be used to implement thumbnails on the page. Now, with. NET, you can easily implement thumbnails. The following is an example of how to implement a thumbnail.

Keywords: C #, ASP. NET, thumbnail

Download instance: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 = "percent" %>
<Html>
<Head>
<Title> Lion interactive network => generating thumbnails </title>
</Head>
<Body>
<Form id = "Form1" method = "post" runat = "server">
</Form>
</Body>
</Html>

 

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. Imaging;
Namespace Exam_C
{
/// <Summary>
/// Summary of ToThumbnailImage.
/// </Summary>
Public class ToThumbnailImage: System. Web. UI. Page
{
/*
Create By lion

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 ";

# Code generated by region Web Form Designer
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 the method for generating thumbnails
ToThumbnailImages ("lionsky.jpg", "B .gif", 300 );
}
# Endregion

# Region Helper
 
/// <Summary>
/// Obtain all relevant information of the image decoder
/// </Summary>
/// <Param name = "mimeType"> a string containing the multi-purpose Internet Mail Extension protocol (MIME) of the decoder </param>
/// <Returns> returns all information about the image decoder. </returns>
Static ImageCodecInfo GetCodecInfo (string mimeType)
{
ImageCodecInfo [] CodecInfo = ImageCodecInfo. GetImageEncoders ();
Foreach (ImageCodecInfo ici in CodecInfo)
{
If (ici. MimeType = mimeType) return ici;
}
Return null;
}

/// <Summary>
/// Check the validity of the extension
/// </Summary>
/// <Param name = "sExt"> file name extension </param>
/// <Returns> If the extension is valid, true is returned; otherwise, false is returned. </returns>
Bool CheckValidExt (string sExt)
{
Bool flag = false;
String [] aExt = AllowExt. Split ('| ');
Foreach (string filetype in aExt)
{
If (filetype. ToLower () = sExt)
{
Flag = true;
Break;
}
}
Return flag;
}

/// <Summary>
/// Save the image
/// </Summary>
/// <Param name = "image"> Image object </param>
/// <Param name = "savePath"> Save path </param>
/// <Param name = "ici"> codec parameters in the specified format </param>
Void SaveImage (System. Drawing. Image image, string savePath, ImageCodecInfo ici)
{
// Set the EncoderParameters object of the original image object
EncoderParameters parameters = new EncoderParameters (1 );
Parameters. Param [0] = new EncoderParameter (Encoder. Quality, (long) 90 ));
Image. Save (savePath, ici, parameters );
Parameters. Dispose ();
}
# Endregion

# Region Methods

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "sourceImagePath"> original image path (relative path) </param>
/// <Param name = "thumbnailImagePath"> the generated thumbnail path. If it is null, save it as the original image path (relative path). </param>
/// <Param name = "thumbnailImageWidth"> width of the thumbnail (the height is automatically generated based on the proportion of the source image) </param>
Public void ToThumbnailImages (string sourceImagePath, string thumbnailImagePath, 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 format of the original image file is incorrect. supported formats include [" + AllowExt + "]", "SourceImagePath ");
}
// Create an Image object from the original Image
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 image proportion
If (double) width)/(double) height)> = 1.33333333333333f)
{
Num = (height * ThumbnailImageWidth)/width );
}
Else
{
ThumbnailImageWidth = (width * num)/height );
}
If (ThumbnailImageWidth <1) | (num <1 ))
{
Return;
}
// Initialize a new Bitmap instance 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 surface and fill it with a transparent background color
Graphics. Clear (Color. Transparent );
// Draw the original image object at the specified position and by the specified size
Graphics. DrawImage (image, new Rectangle (0, 0, ThumbnailImageWidth, num ));
Image. Dispose ();
Try
{
// Save the original image in the specified format and use the specified codec parameter to the specified file
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 ();
}
}
# Endregion

}
}

Source: http://blog.csdn.net/houjianxun/archive/2004/07/07/aaaaa.aspx

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.