2011-09-13[C#生成縮略圖]
/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">源圖路徑(物理路徑)</param>
/// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度</param>
/// <param name="height">縮略圖高度</param>
public void CreateMinImage(string originalImagePath,string thumbnailPath,int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));
int towidth= width;
int toheight= height;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一個bmp圖片
System.Drawing.Image bitmap =new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置並且按指定大小繪製原圖片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0,0, towidth, toheight));
try
{
//以jpg格式保存縮略圖
string FileExt= Path.GetFileNameWithoutExtension(originalImagePath);
//這裏報錯 bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
//修改後的代碼
public void CreateMinImageAndDel(string originalImagePath,string thumbnailPath,int width, int height)
{
Graphics draw =null;
string FileExt= "";
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));
int towidth= width;
int toheight= height;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一個bmp圖片
System.Drawing.Image bitmap =new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Image bitmap2 =new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置並且按指定大小繪製原圖片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0,0, towidth, toheight));
try
{
//以jpg格式保存縮略圖
FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
//用新建立的image對象拷貝bitmap對象 讓g對象可以釋放資源
draw = Graphics.FromImage(bitmap2);
draw.DrawImage(bitmap, 0,0);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
//保存調整在這裏即可
bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt+ ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">源圖路徑(物理路徑)</param>
/// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度</param>
/// <param name="height">縮略圖高度</param>
public void CreateMinImage(string originalImagePath,string thumbnailPath,int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));
int towidth= width;
int toheight= height;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一個bmp圖片
System.Drawing.Image bitmap =new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置並且按指定大小繪製原圖片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0,0, towidth, toheight));
try
{
//以jpg格式保存縮略圖
string FileExt= Path.GetFileNameWithoutExtension(originalImagePath);
//這裏報錯 bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
//修改後的代碼
public void CreateMinImageAndDel(string originalImagePath,string thumbnailPath,int width, int height)
{
Graphics draw =null;
string FileExt= "";
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));
int towidth= width;
int toheight= height;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一個bmp圖片
System.Drawing.Image bitmap =new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Image bitmap2 =new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置並且按指定大小繪製原圖片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0,0, towidth, toheight));
try
{
//以jpg格式保存縮略圖
FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
//用新建立的image對象拷貝bitmap對象 讓g對象可以釋放資源
draw = Graphics.FromImage(bitmap2);
draw.DrawImage(bitmap, 0,0);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
//保存調整在這裏即可
bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt+ ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.