Easily implement thumbnails in dotnet with C #

Source: Internet
Author: User
Tags foreach bool dotnet httpcontext implement relative
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.

Tothumbnailimage.aspx

<%@ Page language= "C #" codebehind= "ToThumbnailImage.aspx.cs" src= "ToThumbnailImage.aspx.cs" autoeventwireup= " False

inherits= "Exam_c.tothumbnailimage"%>
<title>lion Interactive network => generate thumbnails </title>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
</form>
</body>

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; A summary description of the
namespace Exam_c
{
 ///<summary>
 ///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>
 ///Get all the relevant information for the image codec
 ///</ Summary>
 ///<param name= "MimeType" > String </param> with a Multipurpose Internet Mail Extension Protocol (MIME) type containing a codec;
 /// <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>
 ///detect the validity of the extension
 ///</summary>
 ///<param name= "SExt "> FileName extension </param>
 ///<returns> If the extension is valid, returns TRUE, 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 picture path (relative path) </param>
 ///<param name=" Thumbnailimagepath "> Generated 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 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 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

}
}




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.