C # language has a lot to learn, here we mainly introduce C # implementation thumbnails, including the introduction of C # Implementation thumbnails must rely on third-party components and so on.
In the past, C # implementation thumbnails on the page had to rely on third-party components. Now, with. NET, you can easily implement thumbnails in C #.
The following is an example of a C # implementation 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
*/
static Hashtable htmimes=new Hashtable ();
internal readonly string allowext = ". jpe|. Jpeg|. Jpg|. Png|. Tif|. Tiff|. BMP ";
#region The code generated by the Web Forms 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 generation Thumbnail method
tothumbnailimages ("Lionsky.jpg", "B.gif", 300);
}
#endregion
#region Helper
///
<summary>
///gets all the relevant information about the image codec
///
</summary>
///
<param
name= "MimeType"
>
A string of Multipurpose Internet Mail Extension Protocol (MIME) types containing codecs
</param>
///
<returns>
returns 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>
///to detect the validity of the 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"
>
the encoding and decoding parameters for the specified format
</param>
void SaveImage (System.Drawing.Image image,string savepath,imagecodecinfo ici)
{
//Set the EncoderParameters object of 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 picture path (relative path)
</param>
///
<param
name= "Thumbnailimagepath"
>
the resulting thumbnail path,
Save As the original picture path (relative path) if empty
</param>
///
<param
name= "Thumbnailimagewidth"
>
the width of the thumbnail (the 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 incorrect, the support format has [" + Allowext + "]", "Sourceimagepath");
}
//Create image object from 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 the picture
if (((double) width)/((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);
//Clears the entire drawing surface and fills with a transparent background color
Graphics. Clear (color.transparent);
//At the specified location and draw the original picture object 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
}
}