建立圖片的縮略圖

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;

        

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章