rainxiang

莺莺燕燕翠翠红红处处融融洽洽 风风雨雨花花叶叶年年暮暮朝朝

 

缩略图

        /// <summary>
        
/// 缩略图
        
/// </summary>
        
/// <param name="path">图片路径</param>
        
/// <returns>bool</returns>

        public bool souImg(string path)
        
{
            
bool suc = false;
            
double smallwidth = 98//范围宽
            double smallheight = 98//范围高
            double width; //原图宽
            double height; //原图高
            double newwidth; //缩略图宽
            double newheight; //缩略图高
            double n_scale; //压缩比例
            string newpath; //新文件名;
            newpath = path.Replace(".jpg","x.jpg");
            
//从 原图片 创建 Image 对象
            Image image = Image.FromFile(path);
            
//获取缩略比例
            width = image.Width;
            height 
= image.Height;
            
if(width > height)
            
{
                
if (width > smallwidth)
                
{
                    n_scale
=width/smallwidth;
                    newwidth 
= smallwidth;
                }

                
else 
                
{
                    n_scale 
= 1;

                    newwidth 
= width;
                }

                newheight 
= height/n_scale;
        
                
if (newheight > smallheight) 
                
{
                    n_scale 
= newheight/smallheight;
                    newheight 
= smallheight;
                    newwidth 
= smallwidth/n_scale;
                }

            }

            
else
            
{
                
if (height > smallheight)
                
{
                    n_scale
=height/smallheight;
                    newheight 
= smallheight;
                }

                
else 
                
{
                    n_scale 
= 1;
                    newheight 
= height;
                }

                newwidth 
= width/n_scale;
        
                
if (newwidth > smallwidth) 
                
{
                    n_scale 
= newwidth/smallwidth;
                    newwidth 
= smallwidth;
                    newheight 
= smallheight/n_scale;
                }

            }

            
            
//用指定的大小和格式初始化 Bitmap 类的新实例
            Bitmap bitmap  = new Bitmap(Convert.ToInt32(newwidth),Convert.ToInt32(newheight),PixelFormat.Format32bppArgb);
            
//从指定的 Image 对象创建新 Graphics 对象
            Graphics graphics = Graphics.FromImage(bitmap);
            
//清除整个绘图面并以透明背景色填充
            graphics.Clear(Color.Transparent);
            
//在指定位置并且按指定大小绘制 原图片 对象
            graphics.DrawImage(image, new Rectangle(00, Convert.ToInt32(newwidth), Convert.ToInt32(newheight)));
            image.Dispose(); 
            graphics.Dispose();
            
try
            
{
                
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
                bitmap.Save(newpath,ImageFormat.Jpeg);
                suc 
= true;
            }

            
catch
            
{
                suc 
= false;
            }

            
            bitmap.Dispose();

            
return suc;
        }

posted on 2006-04-05 11:10  rainxiang  阅读(514)  评论(0编辑  收藏  举报

导航