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

C#創(chuàng)建縮略圖操作類實(shí)例

 更新時間:2015年04月23日 09:15:37   作者:songguo  
這篇文章主要介紹了C#創(chuàng)建縮略圖操作類,實(shí)例分析了C#創(chuàng)建縮略圖的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下

本文實(shí)例講述了C#創(chuàng)建縮略圖操作類。分享給大家供大家參考。具體分析如下:

這個C#類可以生成各種形式的縮略圖,可以自動保持圖片比例縮略,可以根據(jù)百分比獲得圖片尺寸等

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace HtmlSnap
{
 public static class ImageHelper
 {
  /// <summary>
  /// 獲取縮略圖
  /// </summary>
  /// <param name="image"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <returns></returns>
  public static Image GetThumbnailImage(Image image, int width, int height)
  {
   if (image == null || width < 1 || height < 1)
    return null;
   // 新建一個bmp圖片
   //
   Image bitmap = new System.Drawing.Bitmap(width, height);
   // 新建一個畫板
   //
   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>
  /// <param name="image"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <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>
  /// <param name="picture"></param>
  /// <param name="percent"></param>
  /// <returns></returns>
  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>
  /// <param name="picture"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <returns></returns>
  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>
  /// <param name="image"></param>
  /// <param name="savePath"></param>
  /// <param name="ici"></param>
  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();
  }
 }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • 打開一個Unity工程步驟

    打開一個Unity工程步驟

    這篇文章講述了如何打開一個Unity工程,包含詳細(xì)的圖文介紹的步驟,希望本文對你有所幫助
    2021-06-06
  • C#使用Chart繪制曲線

    C#使用Chart繪制曲線

    這篇文章主要為大家詳細(xì)介紹了C#使用Chart繪制曲線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 利用C#實(shí)現(xiàn)將小數(shù)值四舍五入為整數(shù)

    利用C#實(shí)現(xiàn)將小數(shù)值四舍五入為整數(shù)

    在項(xiàng)目的開發(fā)中,遇到一些除法計算內(nèi)容會產(chǎn)生小數(shù)值,但是又需要根據(jù)項(xiàng)目的實(shí)際情況將這些小數(shù)內(nèi)容化為整數(shù),所以本文為大家整理了C#實(shí)現(xiàn)將小數(shù)值四舍五入為整數(shù)的方法,希望對大家有所幫助
    2023-07-07
  • .Net中的json操作類用法分析

    .Net中的json操作類用法分析

    這篇文章主要介紹了.Net中的json操作類用法分析,是非常實(shí)用的一個技巧,需要的朋友可以參考下
    2014-08-08
  • C#實(shí)現(xiàn)屏幕抓圖并保存的示例代碼

    C#實(shí)現(xiàn)屏幕抓圖并保存的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)屏幕抓圖并保存的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • C#實(shí)現(xiàn)簡單點(diǎn)餐系統(tǒng)

    C#實(shí)現(xiàn)簡單點(diǎn)餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • c# 修改windows中賬戶的用戶名和密碼

    c# 修改windows中賬戶的用戶名和密碼

    這篇文章主要介紹了c# 改變windows中賬戶的用戶名和密碼,幫助大家更好的理解和學(xué)習(xí)C#,感興趣的朋友可以了解下
    2020-11-11
  • unity3D實(shí)現(xiàn)三維物體跟隨鼠標(biāo)

    unity3D實(shí)現(xiàn)三維物體跟隨鼠標(biāo)

    這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)三維物體跟隨鼠標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C#正則檢測字符串是否字母數(shù)字混編的方法

    C#正則檢測字符串是否字母數(shù)字混編的方法

    這篇文章主要介紹了C#正則檢測字符串是否字母數(shù)字混編的方法,涉及C#正則判定字符串的使用技巧,需要的朋友可以參考下
    2015-06-06
  • c#基于winform制作音樂播放器

    c#基于winform制作音樂播放器

    這篇文章主要介紹了c#基于winform制作音樂播放器的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03

最新評論