程序代码
#region 为指定的图片生成缩略图
/// <summary>
/// 为指定的图片生成缩图
/// <para>color != null,则不变形</para>
/// </summary>
/// <param name="sourcepath">源图的完整硬盘路径,包括文件名</param>
/// <param name="maxwidth">缩图宽度</param>
/// <param name="maxheight">缩图高度</param>
/// <param name="color">指定背景色,可传null</param>
/// <param name="bmpath">缩图的完整硬盘目录,以/结束,不包括文件名</param>
/// <returns>是否成功</returns>
public static bool Thumbnail(string sourcepath, int maxwidth, int maxheight, Color color, string bmpath)
{
string filename = Path.GetFileNameWithoutExtension(sourcepath); //拿到没有后缀的文件名
string fileExt = Path.GetExtension(sourcepath).ToLower(); //取得扩展名
if (fileExt.IsNullOrEmpty()) //如果取不到文件后缀,就认为传入路径是错误的
return false;
int ActualWidth = 0; //图片实际宽度
int ActualHeight = 0; //图片实际高度
int Srcx = 0; //图片左边距
int Srcy = 0; //图片上边距
Image newImg;
using (Image oriImg = Image.FromFile(sourcepath))
{
if ((maxwidth < oriImg.Width) || (maxheight < oriImg.Height)) //执行按比例缩小
{
if (maxwidth.toString() / maxheight.toString() > oriImg.Width.toString() / oriImg.Height.toString(0f)) //以高度为准计算
{
ActualWidth = maxheight * oriImg.Width / oriImg.Height;
ActualHeight = maxheight;
Srcx = (maxwidth - ActualWidth) / 2;
}
else
{
ActualWidth = maxwidth;
ActualHeight = maxwidth * oriImg.Height / oriImg.Width;
Srcy = (maxheight - ActualHeight) / 2;
}
}
else
{
ActualWidth = oriImg.Width;
ActualHeight = oriImg.Height;
Srcx = (maxwidth - ActualWidth) / 2;
Srcy = (maxheight - ActualHeight) / 2;
} //以上操作完成后,得到缩图的宽与高,下面生成缩图,并保存到缩图文件夹
//根据颜色判断是否产生固定大小的缩图
newImg = color == null ? new Bitmap(ActualWidth, ActualHeight) : new Bitmap(maxwidth, maxheight);
using (Graphics g = Graphics.FromImage(newImg)) //把新图形加载进Graphice对象中
{
if (color != null)
g.Clear(color); //声明背景色
else
{
Srcx = 0;
Srcy = 0;
}
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic; //指定高质量插值法,关键在于HighQualityBicubic
g.DrawImage(oriImg, new Rectangle(Srcx, Srcy, ActualWidth, ActualHeight), 0, 0, oriImg.Width, oriImg.Height, GraphicsUnit.Pixel);
}
} //到这里,源图,画板均可以释放了
switch (fileExt)
{
case ".jpg":
newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
break;
case ".gif":
newImg.Save(bmpath + filename + ".gif", ImageFormat.Gif);
break;
case ".png":
newImg.Save(bmpath + filename + ".png", ImageFormat.Png);
break;
default:
newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
break;
}
newImg.Dispose();
return true;
}
#endregion
/// <summary>
/// 为指定的图片生成缩图
/// <para>color != null,则不变形</para>
/// </summary>
/// <param name="sourcepath">源图的完整硬盘路径,包括文件名</param>
/// <param name="maxwidth">缩图宽度</param>
/// <param name="maxheight">缩图高度</param>
/// <param name="color">指定背景色,可传null</param>
/// <param name="bmpath">缩图的完整硬盘目录,以/结束,不包括文件名</param>
/// <returns>是否成功</returns>
public static bool Thumbnail(string sourcepath, int maxwidth, int maxheight, Color color, string bmpath)
{
string filename = Path.GetFileNameWithoutExtension(sourcepath); //拿到没有后缀的文件名
string fileExt = Path.GetExtension(sourcepath).ToLower(); //取得扩展名
if (fileExt.IsNullOrEmpty()) //如果取不到文件后缀,就认为传入路径是错误的
return false;
int ActualWidth = 0; //图片实际宽度
int ActualHeight = 0; //图片实际高度
int Srcx = 0; //图片左边距
int Srcy = 0; //图片上边距
Image newImg;
using (Image oriImg = Image.FromFile(sourcepath))
{
if ((maxwidth < oriImg.Width) || (maxheight < oriImg.Height)) //执行按比例缩小
{
if (maxwidth.toString() / maxheight.toString() > oriImg.Width.toString() / oriImg.Height.toString(0f)) //以高度为准计算
{
ActualWidth = maxheight * oriImg.Width / oriImg.Height;
ActualHeight = maxheight;
Srcx = (maxwidth - ActualWidth) / 2;
}
else
{
ActualWidth = maxwidth;
ActualHeight = maxwidth * oriImg.Height / oriImg.Width;
Srcy = (maxheight - ActualHeight) / 2;
}
}
else
{
ActualWidth = oriImg.Width;
ActualHeight = oriImg.Height;
Srcx = (maxwidth - ActualWidth) / 2;
Srcy = (maxheight - ActualHeight) / 2;
} //以上操作完成后,得到缩图的宽与高,下面生成缩图,并保存到缩图文件夹
//根据颜色判断是否产生固定大小的缩图
newImg = color == null ? new Bitmap(ActualWidth, ActualHeight) : new Bitmap(maxwidth, maxheight);
using (Graphics g = Graphics.FromImage(newImg)) //把新图形加载进Graphice对象中
{
if (color != null)
g.Clear(color); //声明背景色
else
{
Srcx = 0;
Srcy = 0;
}
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic; //指定高质量插值法,关键在于HighQualityBicubic
g.DrawImage(oriImg, new Rectangle(Srcx, Srcy, ActualWidth, ActualHeight), 0, 0, oriImg.Width, oriImg.Height, GraphicsUnit.Pixel);
}
} //到这里,源图,画板均可以释放了
switch (fileExt)
{
case ".jpg":
newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
break;
case ".gif":
newImg.Save(bmpath + filename + ".gif", ImageFormat.Gif);
break;
case ".png":
newImg.Save(bmpath + filename + ".png", ImageFormat.Png);
break;
default:
newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
break;
}
newImg.Dispose();
return true;
}
#endregion