C#創(chuàng)建縮略圖操作類實(shí)例
本文實(shí)例講述了C#創(chuàng)建縮略圖操作類。分享給大家供大家參考。具體分析如下:
這個(gè)C#類可以生成各種形式的縮略圖,可以自動(dòng)保持圖片比例縮略,可以根據(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;
// 新建一個(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>
/// <param name="image"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns>生成縮略圖后對(duì)象</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è)定的大小返回圖片的大小,考慮圖片長(zhǎng)寬的比例問題
/// </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è)置 原圖片 對(duì)象的 EncoderParameters 對(duì)象
//
EncoderParameters parms = new EncoderParameters(1);
EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)95));
parms.Param[0] = parm;
image.Save(savePath, ici, parms);
parms.Dispose();
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# 生成高質(zhì)量縮略圖程序—終極算法
- C# 添加圖片水印類實(shí)現(xiàn)代碼
- C# jpg縮略圖函數(shù)代碼
- .net c# gif動(dòng)畫如何添加圖片水印實(shí)現(xiàn)思路及代碼
- c#多圖片上傳并生成縮略圖的實(shí)例代碼
- c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)
- c#利用Grahics進(jìn)行圖片裁剪
- c#生成高清縮略圖的二個(gè)示例分享
- c#圖片處理之圖片裁剪成不規(guī)則圖形
- C#使用GDI+創(chuàng)建縮略圖實(shí)例
- C#獲取視頻某一幀的縮略圖的方法
- C#簡(jiǎn)單生成縮略圖的方法
- C#實(shí)現(xiàn)為一張大尺寸圖片創(chuàng)建縮略圖的方法
- C#圖片處理3種高級(jí)應(yīng)用
相關(guān)文章
c# 使用模式匹配以及 is 和 as 運(yùn)算符安全地進(jìn)行強(qiáng)制轉(zhuǎn)換
這篇文章主要介紹了c# 使用模式匹配以及 is 和 as 運(yùn)算符安全地進(jìn)行強(qiáng)制轉(zhuǎn)換,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-10-10
.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例
這篇文章主要介紹了.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例,本文還包含了取消執(zhí)行和顯示進(jìn)度的例子,需要的朋友可以參考下2014-07-07
C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類,結(jié)合具體實(shí)例形式分析了C#針對(duì)UDP請(qǐng)求的監(jiān)聽、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
使用C#的正則表達(dá)式驗(yàn)證中文字符(實(shí)例代碼)
本文通過實(shí)例代碼給大家介紹了使用C#的正則表達(dá)式驗(yàn)證中文字符的方法,需要的的朋友參考下吧2017-07-07
C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
這篇文章主要介紹了C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C#實(shí)現(xiàn)文件與Base64的相互轉(zhuǎn)換
本文主要介紹了C#實(shí)現(xiàn)文件與Base64的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

