1、如當源圖尺寸過大時,生成的縮略圖質量會很低,而且同源圖的尺寸是一種正比的關係
2、當源圖是一個Gif圖片且含有透明色時,生成的縮略圖會將透明色填充成黑色。
今天在工作中就遇到這樣的問題,基於上面二個原因,於是決定放棄GetThumbnailImage方法了。
最終採用Graphics類的DrawImage方法至於質量問題,適當的設定一下,可以達到很好的效果。
///
/// 文件在服務器上的物理地址
/// 保存在服務器上的路徑
/// 寬度
/// 高度
/// 背景
publicstaticvoidmyGetThumbnailImage(stringSourceFile, stringstrSavePathFile, intThumbWidth, intThumbHeight, stringBgColor)
{
System.Drawing.ImageoImg = System.Drawing.Image.FromFile(SourceFile);
//小圖
intintwidth, intheight;
if(oImg.Width >oImg.Height)
{
if(oImg.Width >ThumbWidth)
{
intwidth = ThumbWidth;
intheight = (oImg.Height * ThumbWidth) / oImg.Width;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
else
{
if(oImg.Height >ThumbHeight)
{
intwidth = (oImg.Width * ThumbHeight) / oImg.Height; intheight = ThumbHeight;
}
else
{
intwidth = oImg.Width; intheight = oImg.Height;
}
}
//構造一個指定寬高的Bitmap
Bitmap bitmay = newBitmap(intwidth, intheight);
Graphics g = Graphics.FromImage(bitmay);
Color myColor;
if(BgColor == null) myColor = Color.FromName("white");
else
myColor = Color.FromName(BgColor);
//用指定的顏色填充Bitmap
g.Clear(myColor);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//開始畫圖
g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);
bitmay.Save(strSavePathFile, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
bitmay.Dispose();
oImg.Dispose();
//刪除源圖
try
{
File.Delete(SourceFile);
}
catch { }
}
本文轉自http://www.lemongtree.com/Archives/559.aspx