91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net中怎么生成縮略圖

發布時間:2021-07-22 16:39:14 來源:億速云 閱讀:129 作者:Leah 欄目:開發技術

本篇文章為大家展示了asp.net中怎么生成縮略圖,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

新建outputimg.ashx

復制代碼 代碼如下:


//調整圖片大小
private static 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)//如果maxWidth和maxHeight大于源圖像,則縮略圖的長和高不變
            {
                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));
        }

復制代碼 代碼如下:


//生成縮略圖
public static void SendSmallImage(string filename, string newfile, int maxHeight, int maxWidth, string mode)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(filename);//源圖像的信息
            System.Drawing.Imaging.ImageFormat thisformat = img.RawFormat; //源圖像的格式
            Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height); //返回調整后的圖像Width與Height
            Bitmap outBmp = new Bitmap(maxWidth, maxHeight);
            Graphics g = Graphics.FromImage(outBmp);
            //設置畫布的描繪質量
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.Clear(Color.White);
            g.DrawImage(img, new Rectangle(((maxWidth - newSize.Width) / 2), ((maxHeight - newSize.Height) / 2), newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            g.Dispose();
            //以下代碼為保存圖片時,設置壓縮質量
            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(newfile, jpegICI, encoderParams);
            }
            else
            {
                outBmp.Save(newfile, thisformat);
            }
            img.Dispose();
            outBmp.Dispose();
        }

輸出圖片:

復制代碼 代碼如下:


//輸出圖片
        public static void OutPutImg(string imgFilePath)
        {
            FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(imgFilePath), FileMode.Open, FileAccess.Read);
            DateTime contentModified = System.IO.File.GetLastWriteTime(HttpContext.Current.Server.MapPath(imgFilePath));
            if (IsClientCached(contentModified))
            {
                HttpContext.Current.Response.StatusCode = 304;
                HttpContext.Current.Response.SuppressContent = true;
            }
            else
            {
                byte[] mydata = new byte[fs.Length];
                int Length = Convert.ToInt32(fs.Length);
                fs.Read(mydata, 0, Length);
                fs.Close();
                HttpContext.Current.Response.OutputStream.Write(mydata, 0, Length);
                HttpContext.Current.Response.ContentType = "image/jpeg";
                HttpContext.Current.Response.End();
                HttpContext.Current.Response.Cache.SetETagFromFileDependencies();
                HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
                HttpContext.Current.Response.Cache.SetLastModified(contentModified);
            }
        }

復制代碼 代碼如下:


//outpuimg.ashx?src=/images/weimeidesc/8af30049-797e-4eb4-8a54-cc4de47c1694.jpg!100x100.jpg
        public void ProcessRequest(HttpContext context)
        {
            //獲取圖片
            string imgUrl = context.Request.QueryString["src"];
            string trueFilePath = imgUrl.Split('!')[0];
            //獲取圖片大小
            int width = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[0]);
            int height = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[1]);

            //圖片已經存在直接輸出
            if (File.Exists(context.Server.MapPath("~/" + imgUrl)))
            {
                OutPutImg("~/"+imgUrl);
            }
            else
            {
                if (!string.IsNullOrEmpty(imgUrl) && File.Exists(context.Server.MapPath("~/" + trueFilePath)))
                {
                    Image originalImage = System.Drawing.Image.FromFile(context.Server.MapPath("~/" + trueFilePath));
                    var newBitmap = new Bitmap(originalImage);
                    //生成相應的小圖并保存
                    SendSmallImage(context.Server.MapPath("~/" + trueFilePath),context.Server.MapPath("~/" + imgUrl), width, height, "meiyouyisi");
                    //輸出
                    OutPutImg("~/" + imgUrl);
                }
                else//圖片如果不存在 輸出默認圖片
                {
                    //OutPutImg(imgUrl);
                }
            }
        }

上述內容就是asp.net中怎么生成縮略圖,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

吉林省| 商都县| 盐亭县| 思茅市| 莎车县| 伊宁市| 北流市| 天长市| 宁夏| 策勒县| 中江县| 平湖市| 敖汉旗| 潮州市| 天镇县| 商河县| 筠连县| 漾濞| 吴桥县| 利辛县| 临武县| 隆子县| 望都县| 黑龙江省| 铁岭市| 敖汉旗| 山阴县| 西吉县| 天镇县| 盐亭县| 沙坪坝区| 平湖市| 清水县| 信丰县| 巍山| 社旗县| 永登县| 普格县| 迁安市| 惠安县| 汶川县|