c#生成圖片縮略圖的類(2種實現(xiàn)思路)
更新時間:2013年05月26日 17:51:09 作者:
4個重載方法,有直接返回Image對象的,有生成縮略圖,并且保存到指定目錄的,具體祥看下文
第一種
/**//// <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;
}
//新建一個bmp圖片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
//新建一個畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質(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();
}
}
關鍵方法Graphics.DrawImage見ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
第二種
4個重載方法,有直接返回Image對象的,有生成縮略圖,并且保存到指定目錄的!
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對象
/// </summary>
/// <param name="Width">縮略圖的寬度</param>
/// <param name="Height">縮略圖的高度</param>
/// <returns>縮略圖的Image對象</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對象
/// </summary>
/// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>
/// <returns>縮略圖的Image對象</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對象
/// </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;
}
}
}
復制代碼 代碼如下:
/**//// <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;
}
//新建一個bmp圖片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
//新建一個畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質(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();
}
}
關鍵方法Graphics.DrawImage見ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
第二種
4個重載方法,有直接返回Image對象的,有生成縮略圖,并且保存到指定目錄的!
復制代碼 代碼如下:
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對象
/// </summary>
/// <param name="Width">縮略圖的寬度</param>
/// <param name="Height">縮略圖的高度</param>
/// <returns>縮略圖的Image對象</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對象
/// </summary>
/// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>
/// <returns>縮略圖的Image對象</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對象
/// </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;
}
}
}
相關文章
Coolite Cool Study 2 同時更新多個Tab
前段時間有一個需求是這樣子的——錄入一個查詢條件, 出來的查詢結(jié)果是多張頁面。不知道有沒朋友遇到這個問題。 展現(xiàn)的效果大概是這個樣子2009-05-05GridView中點擊CheckBox選中一行來改變此行的顏色
這篇文章主要介紹了GridView中點擊CheckBox選中一行來改變此行的顏色的具體實現(xiàn),需要的朋友可以參考下2014-05-05asp.net下xml當作導航數(shù)據(jù)源實現(xiàn)動態(tài)權(quán)限
如果有權(quán)限的話 可以通過節(jié)點的Roles屬性判斷當前登陸的賬號角色名是否符合然后判斷輸出這樣的話您就可以直接操作XML數(shù)據(jù) 而不用考慮別的。2009-12-12在Asp.Net Core中使用ModelConvention實現(xiàn)全局過濾器隔離
這篇文章主要介紹了在Asp.Net Core中使用ModelConvention實現(xiàn)全局過濾器隔離,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01ASP.NET MVC如何使用Unity實現(xiàn)Ioc詳解
這篇文章主要給大家介紹了關于ASP.NET MVC如何使用Unity實現(xiàn)Ioc的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07