準備參數:圖片文件流、文件名數組
方法:1.先將圖片流經過System.Drawing.Image.FromStream方法轉成圖片對象ide
2.經過圖片對象.GetThumbnailImage方法生成自定義長寬的縮略圖。this
生成方法:spa
縮略圖生成:3d
/// <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(600, 400, null, System.IntPtr.Zero); //保存縮略圖到Stream流 thumbImage.Save(stream, this.getImageFormat(suffix)); //關閉縮略圖對象 thumbImage.Dispose(); return stream.ToArray(); }
圖片格式:code
/// <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); }
調用:orm
//調用縮略圖方法 bytes = toImage(ms, ServerUrl);
GIF圖縮略(注:縮略後圖片不能動了,求高手指點)server
// <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(0, 0, width, height); gr.DrawImage(myImage, rectDestination, 0, 0, myImage.Width, myImage.Height, System.Drawing.GraphicsUnit.Pixel); myImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); return stream.ToArray(); }