/// <summary>
/// 保存缩略图
/// </summary>
/// <param name="from">原图路径</param>
/// <param name="to">缩略图虚拟路径</param>
/// <param name="thumbName">原图名称</param>
/// <param name="intThumbWidth">宽</param>
/// <param name="intThumbHeight">高</param>
public static void SaveThumbNail(string from, string to, string thumbName, int intThumbWidth, int intThumbHeight)
{
using (Image sourceImage = Image.FromFile(from))
{
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight; if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
smallWidth = intThumbWidth;
smallHeight = intThumbWidth * height / width;
}
else
{
smallWidth = intThumbHeight * width / height;
smallHeight = intThumbHeight;
} string fileName = intThumbWidth.ToString() + "_" + intThumbHeight.ToString() + "-" + thumbName; string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(to + fileName); using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (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(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//绘制缩略图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}这是我保存缩略图的代码 但是保存缩略图以后 图片的上边框总会出现一条深灰色的线 很难看
我实在找不出原因来 还请大侠们帮忙 感谢了。
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">
/// 生成缩略图的方式
/// "ImageCreateMode.HW" 指定高宽缩放(可能变形)
/// "ImageCreateMode.W" 指定宽,高按比例
/// "ImageCreateMode.H" 指定高,宽按比例
/// "ImageCreateMode.Cut" 指定高宽裁减(不变形)
/// "ImageCreateMode.HWA" 指定一个数值,按比例压缩后,不管是高或是宽都不超过这个值,这时参数Width要和Height一致
/// </param>
public static void MakeThumbnail( Stream ImageStream, string thumbnailPath, int width, int height, ImageCreateMode Mode, System.Drawing.Imaging.ImageFormat SaveImageFormat )
{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream( ImageStream ); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch ( Mode )
{
case ImageCreateMode.HW: #region 指定高宽缩放(可能变形)
#endregion break;
case ImageCreateMode.W: #region 指定宽,高按比例
toheight = ( int )( ( double )originalImage.Height * ( ( double )width / ( double )originalImage.Width ) );
#endregion break;
case ImageCreateMode.H: #region 指定高,宽按比例
towidth = ( int )( ( double )originalImage.Width * ( ( double )height / ( double )originalImage.Height ) );
#endregion break;
case ImageCreateMode.Cut: #region 指定高宽裁减(不变形)
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;
}
#endregion break;
case ImageCreateMode.HWA: #region 指定一个数值,按比例压缩后,不管是高或是宽都不超过这个值,这时参数Width要和Height一致
if ( originalImage.Width > originalImage.Height )
{
toheight = ( int )( ( double )originalImage.Height * ( ( double )width / ( double )originalImage.Width ) );
}
else
{
towidth = ( int )( ( double )originalImage.Width * ( ( double )height / ( double )originalImage.Height ) );
}
#endregion break;
default: #region 指定高宽缩放(可能变形)
#endregion break;
} #region 按指定尺寸生成一个新的对象 //HttpContext.Current.Response.Write( towidth.ToString() );
//新建一个bmp图片
System.Drawing.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 );
#endregion 按指定尺寸生成一个新的对象 #region 保存对象
try
{
//以jpg格式保存缩略图
bitmap.Save( thumbnailPath, SaveImageFormat );
}
catch
{
throw new Exception( thumbnailPath );
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
#endregion 保存对象
}
改为
g.Clear(Color.White);
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxWidth">需要缩小到的宽度</param>
/// <param name="maxHeight">需要缩小到的高度</param>
/// <param name="imageOriginalWidth">图片的原始宽度</param>
/// <param name="imageOriginalHeight">图片的原始高度</param>
/// <returns>返回图片按等比例缩小后的实际大小</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight); if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
} return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
图片处理函数
改成
g.Clear(Color.Transparent); // 改成透明就好了