生活随笔
收集整理的這篇文章主要介紹了
C#生成高清缩略图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| 04 | ???/// <param name="phyPath">原圖片的路徑</param>? |
| 05 | ???/// <param name="width">縮略圖寬</param>? |
| 06 | ???/// <param name="height">縮略圖高</param>? |
| 07 | ???/// <returns></returns>? |
| 08 | ???public?System.Drawing.Image GetThumbnail(System.Drawing.Image image,?int?width,?intheight)? |
| 10 | ???????Bitmap bmp =?newBitmap(width, height);? |
| 11 | ???????//從Bitmap創建一個System.Drawing.Graphics? |
| 12 | ???????System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);? |
| 14 | ???????gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;? |
| 16 | ???????gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;? |
| 18 | ???????gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;? |
| 19 | ???????//把原始圖像繪制成上面所設置寬高的縮小圖? |
| 20 | ???????System.Drawing.Rectangle rectDestination =?newSystem.Drawing.Rectangle(0, 0, width, height);? |
| 22 | ???????gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);? |
?
調用方法
查看源碼 打印?
| 01 | HttpPostedFile file = photoFile.PostedFile;? |
| 02 | ????????if(!file.ContentType.Contains("image"))? |
| 06 | ????????stringext = Path.GetExtension(file.FileName).ToLower();? |
| 07 | ????????if?(ext !=?".jpg"?&& ext !=?".gif"?&& ext !=?".png"&& ext !=?".jpeg")? |
| 09 | return"請您上傳jpg、gif、png圖片";? |
| 11 | ????????if(file.ContentLength > 5 * 1024 * 1024)? |
| 13 | return"請您上傳512字節內的圖片";? |
| 15 | ????????stringnewName = Guid.NewGuid().ToString();? |
| 16 | ????????stringtempPath =?"upload/";? |
| 17 | ????????stringimg = tempPath + newName + ext;? |
| 18 | ????????stringfilePath = Server.MapPath(img);? |
| 19 | ????????if(!Directory.Exists(tempPath))? |
| 21 | ????????????Directory.CreateDirectory(tempPath);? |
| 23 | ????????using(System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))? |
| 25 | ????????????GetThumbnail(originalImage, 504, 374).Save(filePath);? |
轉載于:https://www.cnblogs.com/gc2013/p/3652469.html
總結
以上是生活随笔為你收集整理的C#生成高清缩略图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。