准备参数:图片文件流、文件名
方法:1.先将图片流通过System.Drawing.Image.FromStream方法转成图片对象
2.通过图片对象.GetThumbnailImage方法生成自定义长宽的缩略图。
生成方法:
缩略图生成:
/// <summary>
/// 缩略图
/// shidd
/// </summary>
/// <param name="myStream">文件流</param>
/// <param name="serverUrl">文件名</param>
/// <returns>字节数组</returns>
private byte[] toImage(Stream myStream, string serverUrl)
{
//生成缩略图
//创建改变后的Stream
var stream = new System.IO.MemoryStream();
//取得后缀名
string suffix = serverUrl.Substring(serverUrl.LastIndexOf("."));
//创建一个图像对象取得上传图片对象
System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false);
//int percentage= myImage.Height / myImage.Width;//原图片比例
//由上传图片产生一个缩略图(600*400)
System.Drawing.Image thumbImage = myImage.GetThumbnailImage(, , null, System.IntPtr.Zero);
//保存缩略图到Stream流
thumbImage.Save(stream, this.getImageFormat(suffix));
//关闭缩略图对象
thumbImage.Dispose();
return stream.ToArray();
}
图片格式:
/// <summary>
/// 图片格式
/// </summary>
/// <param name="suffix">文件后缀</param>
/// <returns></returns>
private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
{
System.Drawing.Imaging.ImageFormat myFormat;
switch (suffix.ToLower())
{
case ".bmp":
myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case ".emf":
myFormat = System.Drawing.Imaging.ImageFormat.Emf;
break;
case ".exif":
myFormat = System.Drawing.Imaging.ImageFormat.Exif;
break;
case ".gif":
myFormat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".icon":
myFormat = System.Drawing.Imaging.ImageFormat.Icon;
break;
case ".jpeg":
case ".jpg":
myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png":
myFormat = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".tiff":
myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
break;
case ".wmf":
myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
break;
default:
myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
break;
}
return (myFormat);
}
调用:
//调用缩略图方法
bytes = toImage(ms, ServerUrl);
GIF图缩略(注:缩略后图片不能动了,求高手指点)
// <summary>
/// 为GIF图片生成缩略图
/// </summary>
/// <param name="phyPath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public byte[] GetThumbnail(Stream myStream, int width, int height)
{
//创建改变后的Stream
var stream = new System.IO.MemoryStream();
//创建一个图像对象取得上传图片对象
System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
//从Bitmap创建一个System.Drawing.Graphics
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
//设置
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//把原始图像绘制成上面所设置宽高的缩小图
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(, , width, height); gr.DrawImage(myImage, rectDestination, , , myImage.Width, myImage.Height, System.Drawing.GraphicsUnit.Pixel);
myImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
return stream.ToArray();
}