溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c#中怎么實(shí)現(xiàn)一個(gè)生成圖片縮略圖的類

發(fā)布時(shí)間:2021-07-07 17:59:15 來源:億速云 閱讀:241 作者:Leah 欄目:開發(fā)技術(shù)

c#中怎么實(shí)現(xiàn)一個(gè)生成圖片縮略圖的類,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

第一種

復(fù)制代碼 代碼如下:


/**//// <summary>
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">源圖路徑(物理路徑)</param>
/// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度</param>
/// <param name="height">縮略圖高度</param>
/// <param name="mode">生成縮略圖的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高寬縮放(可能變形)
break;
case "W"://指定寬,高按比例
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H"://指定高,寬按比例
towidth = originalImage.Width * height/originalImage.Height;
break;
case "Cut"://指定高寬裁減(不變形)
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
//新建一個(gè)bmp圖片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
//新建一個(gè)畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}


關(guān)鍵方法Graphics.DrawImage見ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
第二種
4個(gè)重載方法,有直接返回Image對(duì)象的,有生成縮略圖,并且保存到指定目錄的!

復(fù)制代碼 代碼如下:


using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// 圖片處理類
/// 1、生成縮略圖片或按照比例改變圖片的大小和畫質(zhì)
/// 2、將生成的縮略圖放到指定的目錄下
/// </summary>
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// <summary>
/// 類的構(gòu)造函數(shù)
/// </summary>
/// <param name="ImageFileName">圖片文件的全路徑名稱</param>
public ImageClass(string ImageFileName)
{
ResourceImage=Image.FromFile(ImageFileName);
ErrMessage="";
}
public bool ThumbnailCallback()
{
return false;
}
/// <summary>
/// 生成縮略圖重載方法1,返回縮略圖的Image對(duì)象
/// </summary>
/// <param name="Width">縮略圖的寬度</param>
/// <param name="Height">縮略圖的高度</param>
/// <returns>縮略圖的Image對(duì)象</returns>
public Image GetReducedImage(int Width,int Height)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
/// <summary>
/// 生成縮略圖重載方法2,將縮略圖文件保存到指定的路徑
/// </summary>
/// <param name="Width">縮略圖的寬度</param>
/// <param name="Height">縮略圖的高度</param>
/// <param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:Images ilename.jpg</param>
/// <returns>成功返回true,否則返回false</returns>
public bool GetReducedImage(int Width,int Height,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
/// <summary>
/// 生成縮略圖重載方法3,返回縮略圖的Image對(duì)象
/// </summary>
/// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>
/// <returns>縮略圖的Image對(duì)象</returns>
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
return ReducedImage;
}
catch(Exception e)
{
ErrMessage=e.Message;
return null;
}
}
/// <summary>
/// 生成縮略圖重載方法4,返回縮略圖的Image對(duì)象
/// </summary>
/// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>
/// <param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:Images ilename.jpg</param>
/// <returns>成功返回true,否則返回false</returns>
public bool GetReducedImage(double Percent,string targetFilePath)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);
ReducedImage.Dispose();
return true;
}
catch(Exception e)
{
ErrMessage=e.Message;
return false;
}
}
}

看完上述內(nèi)容,你們掌握c#中怎么實(shí)現(xiàn)一個(gè)生成圖片縮略圖的類的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI