欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net等比縮放生成縮略圖的方法

 更新時(shí)間:2015年11月27日 08:50:33   投稿:hebedich  
本文給大家匯總了2個(gè)C#中等比縮放實(shí)現(xiàn)生成縮略圖的方法,第一種稍微簡單些,第二種是本人常用的方法,這里推薦給大家,有需要的小伙伴可以參考下。

生成縮略圖是一個(gè)十分常用功能,找到了一個(gè)方法,重寫部分代碼,實(shí)用又好用,.net又一個(gè)生成縮略圖的方法,不變形

/// <summary> 
   /// 為圖片生成縮略圖
   /// </summary> 
   /// <param name="phyPath">原圖片的路徑</param> 
   /// <param name="width">縮略圖寬</param> 
   /// <param name="height">縮略圖高</param> 
   /// <returns></returns> 
   public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height)
   {
     Bitmap m_hovertreeBmp = new Bitmap(width, height);
     //從Bitmap創(chuàng)建一個(gè)System.Drawing.Graphics 
     Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp);
     //設(shè)置 
     m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     //下面這個(gè)也設(shè)成高質(zhì)量 
     m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
     //下面這個(gè)設(shè)成High 
     m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
     //把原始圖像繪制成上面所設(shè)置寬高的縮小圖 
     Rectangle rectDestination = new Rectangle(0, 0, width, height);

     int m_width, m_height;
     if (image.Width * height > image.Height * width)
     {
       m_height = image.Height;
       m_width = (image.Height * width) / height;
     }
     else
     {
       m_width = image.Width;
       m_height = (image.Width * height) / width;
     }

     m_HvtGr.DrawImage(image, rectDestination, 0, 0, m_width, m_height, GraphicsUnit.Pixel);

     return m_hovertreeBmp;
   }

C#縮略圖生成類,采用高質(zhì)量插值法實(shí)現(xiàn)縮略圖生成,高質(zhì)量,低速度呈現(xiàn)平滑程度,可以保持縮略圖縱橫比,在獲取縮略圖的時(shí)候一開始就根據(jù)百分比獲取圖片的尺寸、根據(jù)設(shè)定的大小返回圖片的大小,并高質(zhì)量保存縮略圖圖片,為原圖片設(shè)置EncoderParameters 對象。

以下為類文件,建議保存文件名為:ImageHelper.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace HtmlSnap
{
  public static class ImageHelper
  {
    /// 獲取縮略圖
    public static Image GetThumbnailImage(Image image, int width, int height)
    {
      if (image == null || width < 1 || height < 1)
        return null;
      // 新建一個(gè)bmp圖片
      Image bitmap = new System.Drawing.Bitmap(width, height);

      // 新建一個(gè)畫板
      using (Graphics g = System.Drawing.Graphics.FromImage(bitmap))
      {

        // 設(shè)置高質(zhì)量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        // 設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        
        // 高質(zhì)量、低速度復(fù)合
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        
 // 清空畫布并以透明背景色填充
        g.Clear(Color.Transparent);
        
 // 在指定位置并且按指定大小繪制原圖片的指定部分
        g.DrawImage(image, new Rectangle(0, 0, width, height),
          new Rectangle(0, 0, image.Width, image.Height),
          GraphicsUnit.Pixel);
        return bitmap;
      }
    }
    /// <summary>
    /// 生成縮略圖,并保持縱橫比
    /// </summary>
    /// <returns>生成縮略圖后對象</returns>
    public static Image GetThumbnailImageKeepRatio(Image image, int width, int height)
    {
      Size imageSize = GetImageSize(image, width, height);
      return GetThumbnailImage(image, imageSize.Width, imageSize.Height);
    }

    /// <summary>
    /// 根據(jù)百分比獲取圖片的尺寸
    /// </summary>
    public static Size GetImageSize(Image picture, int percent)
    {
      if (picture == null || percent < 1)
        return Size.Empty;

      int width = picture.Width * percent / 100;
      int height = picture.Height * percent / 100;

      return GetImageSize(picture, width, height);
    }
    /// <summary>
    /// 根據(jù)設(shè)定的大小返回圖片的大小,考慮圖片長寬的比例問題
    /// </summary>
    public static Size GetImageSize(Image picture, int width, int height)
    {
      if (picture == null || width < 1 || height < 1)
        return Size.Empty;
      Size imageSize;
      imageSize = new Size(width, height);
      double heightRatio = (double)picture.Height / picture.Width;
      double widthRatio = (double)picture.Width / picture.Height;
      int desiredHeight = imageSize.Height;
      int desiredWidth = imageSize.Width;
      imageSize.Height = desiredHeight;
      if (widthRatio > 0)
        imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio);
      if (imageSize.Width > desiredWidth)
      {
        imageSize.Width = desiredWidth;
        imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio);
      }
      return imageSize;
    }
    /// <summary>
    /// 獲取圖像編碼解碼器的所有相關(guān)信息
    /// </summary>
    /// <param name="mimeType">包含編碼解碼器的多用途網(wǎng)際郵件擴(kuò)充協(xié)議 (MIME) 類型的字符串</param>
    /// <returns>返回圖像編碼解碼器的所有相關(guān)信息</returns>
    public static ImageCodecInfo GetCodecInfo(string mimeType)
    {
      ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo ici in CodecInfo)
      {
        if (ici.MimeType == mimeType) return ici;
      }
      return null;
    }
    public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
    {
      ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo icf in encoders)
      {
        if (icf.FormatID == format.Guid)
        {
          return icf;
        }
      }
      return null;
    }
    public static void SaveImage(Image image, string savePath, ImageFormat format)
    {
      SaveImage(image, savePath, GetImageCodecInfo(format));
    }
    /// <summary>
    /// 高質(zhì)量保存圖片
    /// </summary>
    private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
    {
      // 設(shè)置 原圖片 對象的 EncoderParameters 對象
      EncoderParameters parms = new EncoderParameters(1);
      EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95));
      parms.Param[0] = parm;
      image.Save(savePath, ici, parms);
      parms.Dispose();
    }

  }
}

相關(guān)文章

  • asp.net Mvc4 使用ajax結(jié)合分頁插件實(shí)現(xiàn)無刷新分頁

    asp.net Mvc4 使用ajax結(jié)合分頁插件實(shí)現(xiàn)無刷新分頁

    本篇文章主要介紹了 asp.net Mvc4 使用ajax結(jié)合分頁插件實(shí)現(xiàn)無刷新分頁,ajax通過回調(diào)函數(shù)把控制器返回的分部視圖內(nèi)容加載到主視圖中顯示,有興趣的可以了解一下。
    2017-01-01
  • asp.net 文件下載實(shí)現(xiàn)代碼

    asp.net 文件下載實(shí)現(xiàn)代碼

    文件下載代碼,Copy 別人的.需要的朋友可以參考下。
    2010-03-03
  • AutoCAD .Net禁止圖元被刪除的方法

    AutoCAD .Net禁止圖元被刪除的方法

    這篇文章主要為大家詳細(xì)介紹了AutoCAD .Net禁止圖元被刪除的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Entity?Framework?Core關(guān)聯(lián)刪除

    Entity?Framework?Core關(guān)聯(lián)刪除

    關(guān)聯(lián)刪除通常是一個(gè)數(shù)據(jù)庫術(shù)語,用于描述在刪除行時(shí)允許自動觸發(fā)刪除關(guān)聯(lián)行的特征;即當(dāng)主表的數(shù)據(jù)行被刪除時(shí),自動將關(guān)聯(lián)表中依賴的數(shù)據(jù)行進(jìn)行刪除,或者將外鍵更新為NULL或默認(rèn)值。本文將為大家具體介紹一下Entity?Framework?Core關(guān)聯(lián)刪除,需要的可以參考一下
    2021-12-12
  • ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼

    ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼

    這篇文章給大家介紹了asp.net中畫圖形驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-01-01
  • MVC+EasyUI+三層新聞網(wǎng)站建立 詳情頁面制作方法(八)

    MVC+EasyUI+三層新聞網(wǎng)站建立 詳情頁面制作方法(八)

    這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第八篇,教大家如何制作詳情頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁實(shí)現(xiàn)代碼

    Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁實(shí)現(xiàn)代碼

    今天與大家分享一下“Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁”首先聲明以下幾點(diǎn)
    2012-01-01
  • ASP.NET Core Authentication認(rèn)證實(shí)現(xiàn)方法

    ASP.NET Core Authentication認(rèn)證實(shí)現(xiàn)方法

    這篇文章主要介紹了ASP.NET Core Authentication認(rèn)證實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • asp.net 打印控件使用方法

    asp.net 打印控件使用方法

    做過很多的Web項(xiàng)目,大多數(shù)在打印頁面內(nèi)容的時(shí)候,采用的都是通過Javascript調(diào)用系統(tǒng)內(nèi)置的打印方法進(jìn)行打印,也就是調(diào)用 PrintControl.ExecWB(?,?)實(shí)現(xiàn)直接打印和打印預(yù)覽功能。
    2010-01-01
  • Web.Config文件配置之限制上傳文件大小和時(shí)間的屬性配置

    Web.Config文件配置之限制上傳文件大小和時(shí)間的屬性配置

    在Web.Config文件中配置限制上傳文件大小與時(shí)間字符串時(shí),是在httpRuntime httpRuntime節(jié)中完成的,需要設(shè)置以下2個(gè)屬性:maxRequestLength屬性與ExecutionTimeout屬性,感興趣的朋友可以了解下,或許對你有所幫助
    2013-02-02

最新評論