生成图片缩略图的问题
Bitmap tempBitmap = new Bitmap(w, h);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempBitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality =System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, 230, 230);
g.DrawImage(img, rectDestination, 0, 0, 800, 800, GraphicsUnit.Pixel);
tempBitmap.Save(SmallPath);
tempBitmap.Dispose();
img.Dispose();
采用这种方法生成的缩略图片的大小(不是说图片尺寸)太大,有时比原图片还要大,请问有没有什么方法解决
[解决办法]
tempBitmap.Save(SmallPath);
看看你保存成什么格式文件了!
另外在代码中设置一些参数,例如所支持的颜色数量、你写的HighQuality等等,这些都是增大尺寸的原因。
[解决办法]
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourcePicture">源图(绝对路径)</param>
/// <param name="targetPicture">缩略图(绝对路径)</param>
/// <param name="thunbmailWidth">缩略图宽度</param>
/// <param name="thunbmailHeight">缩略图高度</param>
/// <param name="makeModel">生成缩略图的方式(可用值为1:指定高宽缩放,可能变形;2:指定宽,高按比例;3:指定高,宽按比例;4:指定高宽裁减,不变形;)</param>
public static void MakeThumbnail(string sourcePicture, string targetPicture, int thunbmailWidth, int thunbmailHeight, int makeModel)
{
System.Drawing.Image m_imgSource = System.Drawing.Image.FromFile(sourcePicture);
int m_iTargetWidth = thunbmailWidth;
int m_iTargetHeight = thunbmailHeight;
int x = 0;
int y = 0;
int m_iSourceWidth = m_imgSource.Width;
int m_iSourceHeight = m_imgSource.Height;
switch (makeModel)
{
case 1://指定高宽缩放(可能变形)
break;
case 2://指定宽,高按比例
m_iTargetHeight = m_imgSource.Height * thunbmailWidth / m_imgSource.Width;
break;
case 3://指定高,宽按比例
m_iTargetWidth = m_imgSource.Width * thunbmailHeight / m_imgSource.Height;
break;
case 4://指定高宽裁减(不变形)
if ((double)m_imgSource.Width / (double)m_imgSource.Height > (double)m_iTargetWidth / (double)m_iTargetHeight)
{
m_iSourceWidth = m_imgSource.Height * m_iTargetWidth / m_iTargetHeight;
m_iSourceHeight = m_imgSource.Height;
y = 0;
x = (m_imgSource.Width - m_iSourceWidth) / 2;
}
else
{
m_iSourceWidth = m_imgSource.Width;
m_iSourceHeight = m_imgSource.Width * thunbmailHeight / m_iTargetWidth;
x = 0;
y = (m_imgSource.Height - m_iSourceHeight) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image m_imgTarget = new System.Drawing.Bitmap(m_iTargetWidth, m_iTargetHeight);
//新建一个画板
System.Drawing.Graphics m_grTarget = System.Drawing.Graphics.FromImage(m_imgTarget);
//设置高质量插值法
m_grTarget.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
m_grTarget.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
m_grTarget.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
m_grTarget.DrawImage(m_imgSource, new System.Drawing.Rectangle(0, 0, m_iTargetWidth, m_iTargetHeight),
new System.Drawing.Rectangle(x, y, m_iSourceWidth, m_iSourceHeight),
System.Drawing.GraphicsUnit.Pixel);
ImageFormat m_imgTargetFormat = ImageFormat.Jpeg;
switch (Right(targetPicture, 4).ToLower())
{
case ".gif":
m_imgTargetFormat = ImageFormat.Gif; break;
case ".jpg":
m_imgTargetFormat = ImageFormat.Jpeg; break;
case ".png":
m_imgTargetFormat = ImageFormat.Png; break;
case ".bmp":
m_imgTargetFormat = ImageFormat.Bmp; break;
default:
m_imgTargetFormat = ImageFormat.Jpeg; break;
}
try
{
//以jpg格式保存缩略图
m_imgTarget.Save(targetPicture, m_imgTargetFormat);
}
catch (System.Exception e)
{
throw e;
}
finally
{
m_imgSource.Dispose();
m_imgTarget.Dispose();
m_grTarget.Dispose();
}
}
