建立图片的缩略图

public class ImportFileHelper{

   public static Image vreateThumbnaulImage(string fileImagepath ,Size size){

      using(FileStream file = File.OpenRead(fileImagepath)){

         Stream fileStream = file;

        Image rawImage = Image.FromStream(fileStream,true);//从文件取得图片对象,并使用流中嵌入的颜色管理信息


       //缩略图宽、高

        double newWidth = rawImage.Width, newHeight = rawImage.Height;

        if(rawImage.Width >= rawImage.Height  && rawImage.Width>size.Width){

            newWidth =size.Width;//宽按模版,高按比例缩放

            newHeight = rawImage.Height*(newWidth/rawImage.Width);

        }

        else{

           if(rawImage.Height>size.Height){

                   newHeight = size.Height; //高按模版,宽按比例缩放

                  newWidth=rawImage.Width*(newHeight/rawImage.Height);

           }

       }

      Image thumbnail = new Bitmap((int)newWidth,(int)newHeight); // 新建缩略图片

     Graphics g = Graphics.FromImage(thumbnail);// 新建一个画板

      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Hight;//设置高质量插值法

     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度

    g.Clear(Color.White);//清空一下画布  
  

   g.DrawImage(rawImage,

                            new Rectangle(0,0,thumbnail.Width,thumbnail.Height),// 目标区域

                             new Rectangle(0,0,rawImage.Width,rawImage.Height), // 源区域

                            GraphicsUnit.Pixel);


    rawImage.Dispose();

    g.Dispose();

     return thumbnail;

        

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值