日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET高质量生成缩略图

發布時間:2025/3/20 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET高质量生成缩略图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?//調用方法
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using CLJIMG;

?System.Drawing.Image image = System.Drawing.Image.FromStream(FileUp.PostedFile.InputStream);
??????????????????? int width = image.Width;
??????????????????? int height = image.Height;

??????????????????? if (width > 760)
??????????????????? {
??????????????????????? width = 760;
??????????????????????? height = image.Height * 760 / image.Width;

??????????????????????? newwidth = 760;
??????????????????????? newheight = image.Height * newwidth / image.Width;

??????????????????????? // SendSmallImage(image, filename, newwidth, newheight);
??????????????????????? CLJIMG.ImgClass simg = new ImgClass();
??????????????????????? simg.SendSmallImage(image, Server.MapPath(filename), newwidth, newheight);

??????????????????? }


?//生成高質量縮略圖
#region
private Size NewSize(int maxWidth, int maxHeight, int width, int height)
??? {
??????? double w = 0.0;
??????? double h = 0.0;
??????? double sw = Convert.ToDouble(width);
??????? double sh = Convert.ToDouble(height);
??????? 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));
??? }
??? //生成高質量縮略圖
??? private void SendSmallImage(System.Drawing.Image img, string fileName, int maxWidth, int maxHeight)
??? {
??????? //? System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
??????? System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;

??????? Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
??????? Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
??????? Graphics g = Graphics.FromImage(outBmp);

??????? // 設置畫布的描繪質量

??????? g.CompositingQuality = CompositingQuality.HighQuality;
??????? g.SmoothingMode = SmoothingMode.HighQuality;
??????? g.InterpolationMode = InterpolationMode.HighQualityBicubic;

??????? g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
??????????? 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
??????? g.Dispose();

??????? if (thisFormat.Equals(ImageFormat.Gif))
??????? {
??????????? Response.ContentType = "image/gif";
??????? }
??????? else
??????? {
??????????? Response.ContentType = "image/jpeg";
??????? }

??????? // 以下代碼為保存圖片時,設置壓縮質量
??????? EncoderParameters encoderParams = new EncoderParameters();
??????? long[] quality = new long[1];
??????? quality[0] = 100;

??????? EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
??????? encoderParams.Param[0] = encoderParam;

??????? //獲得包含有關內置圖像編碼解碼器的信息的ImageCodecInfo 對象。
??????? ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
??????? ImageCodecInfo jpegICI = null;
??????? for (int x = 0; x < arrayICI.Length; x++)
??????? {
??????????? if (arrayICI[x].FormatDescription.Equals("JPEG"))
??????????? {
??????????????? jpegICI = arrayICI[x];//設置JPEG編碼
??????????????? break;
??????????? }
??????? }

??????? if (jpegICI != null)
??????? {
??????????? // outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
??????????? outBmp.Save(Server.MapPath(fileName), jpegICI, encoderParams);
??????? }
??????? else
??????? {
??????????? outBmp.Save(Response.OutputStream, thisFormat);
??????????? outBmp.Save(Server.MapPath(fileName), thisFormat);
??????? }

??????? img.Dispose();
??????? outBmp.Dispose();
??? }
#endregion

總結

以上是生活随笔為你收集整理的ASP.NET高质量生成缩略图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。