/**/
///
<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);
// 設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// 設置高質量,低速度呈現平滑程度
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();
}
}
/// 生成縮略圖
/// </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);
// 設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// 設置高質量,低速度呈現平滑程度
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();
}
}
第二種
4個重載方法,有直接返回Image對象的,有生成縮略圖,並且保存到指定目錄的!
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// 圖片處理類
/// 1、生成縮略圖片或按照比例改變圖片的大小和畫質
/// 2、將生成的縮略圖放到指定的目錄下
/// </summary>
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// <summary>
/// 類的構造函數
/// </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"> 縮略圖保存的全文件名,(帶路徑),參數格式:D:\Images\filename.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"> 縮略圖保存的全文件名,(帶路徑),參數格式:D:\Images\filename.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;
}
}
}
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// 圖片處理類
/// 1、生成縮略圖片或按照比例改變圖片的大小和畫質
/// 2、將生成的縮略圖放到指定的目錄下
/// </summary>
public class ImageClass
{
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// <summary>
/// 類的構造函數
/// </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"> 縮略圖保存的全文件名,(帶路徑),參數格式:D:\Images\filename.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"> 縮略圖保存的全文件名,(帶路徑),參數格式:D:\Images\filename.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;
}
}
}
