Before thumbnails, implementing thumbnails on a page must be aided by Third-party components. Now that you have. NET, you can easily implement thumbnails. Here is an example of implementing a thumbnail.
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 description of the tothumbnailimage.
</summary>
public class ToThumbnailImage:System.Web.UI.Page
{
/*
Create by Lion
2003-05-20 19:00
Copyright (C) www.LionSky.Net. All rights reserved.
Web:http://www.lionsky.net;
Email:lion-a@sohu.com
*/
<summary>
Get all the relevant information about the image codec
</summary>
<param name= "MimeType" > A Multipurpose Internet Mail Extension Protocol (MIME) type containing codecs </param>
<returns> return all relevant information about the image codec </returns>
Static ImageCodecInfo Getcodecinfo (String mimetype)
{
imagecodecinfo[] Codecinfo = Imagecodecinfo.getimageencoders ();
foreach (ImageCodecInfo ici in codecinfo)
{
if (ICI. MimeType = = mimetype) return ici;
}
return null;
}
<summary>
Detecting the validity of an extension
</summary>
<param name= "SEXT" > FileName extension </param>
<returns> returns True if the extension is valid, otherwise returns false.</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 picture
</summary>
<param name= "image" >image object </param>
<param name= "Savepath" > Save path </param>
<param name= "ICI" > specified format for codec parameters </param>
void SaveImage (System.Drawing.Image image,string savepath,imagecodecinfo ici)
{
Set the EncoderParameters object for the original picture 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 thumbnails
</summary>
<param name= "Sourceimagepath" > Original image Path (relative path) </param>
<param name= "Thumbnailimagepath" > The resulting thumbnail path, if empty, save as the original picture path (relative path) </param>
<param name= "Thumbnailimagewidth" > Thumbnail width (height is automatically generated by the proportion of the source picture) </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 original picture file format is not correct, the support 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 proportions of a picture
if (((double)/((double) height) >= 1.3333333333333333f)
{
num = ((height * thumbnailimagewidth)/width);
}
Else
{
Thumbnailimagewidth = ((width * num)/height);
}
if ((Thumbnailimagewidth < 1) | | (num < 1))
{
Return
}
Initializes a new instance of the Bitmap class with the specified size and format
Bitmap Bitmap = new Bitmap (thumbnailimagewidth, num, Pixelformat.format32bppargb);
Creates a new Graphics object from the specified Image object
Graphics Graphics = graphics.fromimage (bitmap);
Clear the entire drawing surface and fill with a transparent background color
Graphics. Clear (color.transparent);
Draws the original picture object at the specified location and at the specified size
Graphics. DrawImage (Image, new Rectangle (0, 0, thumbnailimagewidth, num));
Image. Dispose ();
Try
{
Saves the original picture in the specified format and with 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
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.