生成缩略图

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Drawing;

public   class  PictureSlightly
{
    
public  PictureSlightly()
    {
        
//
        
//  TODO: Add constructor logic here
        
//
    }
    
///   <summary>
    
///  生成缩略图
    
///   </summary>
    
///   <param name="SourceImage"> 源图路径(含图片,物理路径) </param>
    
///   <param name="ImageMapth"> 缩略图路径(含图片,物理路径) </param>
    
///   <param name="width"> 缩略图宽 </param>
    
///   <param name="height"> 缩略图高 </param>
     public   static   void  MakeImage( string  sourceImage,  string  imageMapth,  int  width,  int  height,  string  mode)
    {
        System.Drawing.Image MyImage 
=  System.Drawing.Image.FromFile(sourceImage);
        
int  towidth  =  width;
        
int  toheight  =  height;
        
int  x  =   0 , y  =   0 ;
        
int  ow  =  MyImage.Width;
        
int  oh  =  MyImage.Height;
        
switch  (mode)
        {
            
case   " hw " break ;
            
case   " w " : toheight  =  MyImage.Height  *  width  /  MyImage.Width;  break ;
            
case   " h " : towidth  =  MyImage.Width  *  height  /  MyImage.Height;  break ;
            
case   " cut " :
                
if  (( double )MyImage.Width  /  ( double )MyImage.Height  >  ( double )towidth  /  ( double )toheight)
                {
                    oh 
=  MyImage.Height;
                    ow 
=  MyImage.Width  *  towidth  /  toheight;
                    y 
=   0 ;
                    x 
=  (MyImage.Width  -  ow)  /   2 ;
                }
                
else
                {
                    ow 
=  MyImage.Width;
                    oh 
=  MyImage.Height  *  height  /  toheight;
                    x 
=   0 ;
                    y 
=  (MyImage.Height  -  oh)  /   2 ;
                }; 
break ;
            
default break ;
        }

        System.Drawing.Image bitmap 
=   new  System.Drawing.Bitmap(towidth, toheight);
        Graphics g 
=  Graphics.FromImage(bitmap);
        g.InterpolationMode 
=  System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode 
=  System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(MyImage, 
new  Rectangle( 0 0 , towidth, toheight),  new  Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
        
try
        {
            bitmap.Save(imageMapth, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        
catch
        {
        }
        MyImage.Dispose();
        bitmap.Dispose();
        g.Dispose();
    }
}

你可能感兴趣的:(缩略图)