婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > asp.net生成縮略圖示例方法分享

asp.net生成縮略圖示例方法分享

熱門標簽:ai電銷機器人對貸款有幫助嗎 400電話辦理信任翰諾科技 電銷機器人 數據 宿遷智能外呼系統排名 福州人工智能電銷機器人加盟 怎樣給陜西地圖標注顏色 云狐人工智能電話機器人 廣州銷售外呼系統定制 地圖標注多少錢一張

做站的時候經常會遇到要生成縮略圖的功能,因為可能不同的情況需要用來不同大小的縮略圖。

本文生成的圖片都為正方形,只有正方形的縮略圖才是保證圖片足夠清晰。

當我我這里說的正方形是先按比例壓縮,然后加一個固定的白底 然后居中顯示。

代碼:

新建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創建動態縮略圖的方法
  • asp.net中生成縮略圖并添加版權實例代碼
  • asp.net生成縮略圖實現代碼
  • asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
  • asp.net 生成縮略圖代碼
  • asp.net 上傳圖片并同時生成縮略圖的代碼
  • asp.net 點縮略圖彈出隨圖片大小自動調整的頁面
  • ASP.Net 上傳圖片并生成高清晰縮略圖
  • asp.net生成高質量縮略圖通用函數(c#代碼),支持多種生成方式
  • ASP.NET中高質量縮略圖的生成代碼
  • asp.net圖片上傳生成縮略圖的注意事項
  • ASP.NET實現根據URL生成網頁縮略圖的方法

標簽:焦作 宜春 曲靖 新疆 延安 綿陽 大興安嶺 黃南

巨人網絡通訊聲明:本文標題《asp.net生成縮略圖示例方法分享》,本文關鍵詞  asp.net,生成,縮,略圖,示例,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net生成縮略圖示例方法分享》相關的同類信息!
  • 本頁收集關于asp.net生成縮略圖示例方法分享的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 原阳县| 逊克县| 泸州市| 社旗县| 聊城市| 林芝县| 宁武县| 常熟市| 弋阳县| 宜都市| 二手房| 措勤县| 惠安县| 阳城县| 阿克苏市| 桐乡市| 青浦区| 黔南| 武宣县| 翁牛特旗| 雷山县| 福建省| 玉环县| 通城县| 浦城县| 新邵县| 元朗区| 邛崃市| 平塘县| 砚山县| 南丰县| 兴国县| 南投市| 云阳县| 仙桃市| 蒙自县| 兰坪| 宁化县| 巴中市| 景泰县| 宽甸|