>>生如夏花<<

该写些什么了......

生成缩略图的一个方法

    private static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        
{
            System.Drawing.Image originalImage 
= System.Drawing.Image.FromFile(originalImagePath);
            
int towidth = width;
            
int toheight = height;
            
int x = 0;
            
int y = 0;
            
int ow = originalImage.Width;
            
int oh = originalImage.Height;        
            
switch (mode)
            
{        
                
case "HW":               
                    
break;
                
case "W":                    
                    toheight 
= originalImage.Height * width/originalImage.Width;
                    
break;
                
case "H":
                    towidth 
= originalImage.Width * height/originalImage.Height;                    
                    
break;        
                
case "Cut":                
                    
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
                    
{
                        oh 
= originalImage.Height;
                        ow 
= originalImage.Height*towidth/toheight;
                        y 
= 0;
                        x 
= (originalImage.Width - ow)/2;
                    }

                    
else
                    
{
                        ow 
= originalImage.Width;
                        oh 
= originalImage.Width*height/towidth;
                        x 
= 0;
                        y 
= (originalImage.Height - oh)/2;
                    }

                    
break;                    
                
default :
                    
break;
            }
    
            System.Drawing.Image bitmap 
= new System.Drawing.Bitmap(towidth,toheight);
            Graphics g 
= System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode 
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);        
            g.DrawImage(originalImage, 
new Rectangle(00, towidth, toheight), 
                
new Rectangle(x, y, ow,oh),
                GraphicsUnit.Pixel);
            
try
            
{            
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            
catch(System.Exception e)
            
{
                
throw e;
            }

            
finally
            
{
                originalImage.Dispose();
                bitmap.Dispose();                        
                g.Dispose();
            }

        }
需引用命名空间 System.Drawing;

posted on 2007-04-07 11:13 wilensky 阅读(272) 评论(2)  编辑 收藏

评论

#1楼  2007-04-07 11:34 yunhuasheng      

good   回复  引用  查看    

#2楼  2007-04-07 11:44 kaweh [未注册用户]

#region
嘿嘿。。。。
#endregion   回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-04-07 11:15 编辑过
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: