ASP.NET 產生縮圖

來源:互聯網
上載者:User

以下代碼根據產生圖片的寬、高,產生四種模式的圖片縮圖

using System;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.IO; public partial class ImgOperator_ImageCut : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        MakeThumbImage("~/images/DSC01010.JPG", "~/images/cut5.jpg", 800, 450, "BGCUT");    }     private void MakeThumbImage(string initPath, string outPath, int width, int height, string mode)    {        System.Drawing.Image image=System.Drawing.Image.FromFile(Server.MapPath(initPath));        int tw = width;        int th = height;         //產生的圖片大小        int sw = image.Width;        int sh = image.Height;        int x = 0;        int y = 0;         switch (mode)        {             case "HW":                break;            case "W":                th = width * image.Height / image.Width;                break;            case "H":                tw = height * image.Width / image.Height;                break;            case "CUT":                if ((double)sw / (double)sh < (double)width / (double)height)                {                    sw = image.Width;                    sh = image.Width * th / tw;                    x = 0;                    y = (image.Height - sh) / 2;                }                else                {                    sh = image.Height;                    sw = image.Height * tw / th;                    x = (image.Width - sw) / 2;                    y = 0;                }                break;            case "BGCUT":                if ((double)width / (double)height < (double)image.Width / (double)image.Height)                {                    sw = image.Width;                    sh = image.Width * th / tw;                    x = 0;                    y = (image.Height - sh) / 2;                }                else                {                    sh = image.Height;                    sw = image.Height * tw / th;                    x = (image.Width - sw) / 2;                    y = 0;                }                break;            default: break;        }        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tw, th);        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        g.Clear(System.Drawing.Color.WhiteSmoke);         g.DrawImage(image,new System.Drawing.Rectangle(0,0,tw,th),new System.Drawing.Rectangle(x,y,sw,sh),System.Drawing.GraphicsUnit.Pixel);         try        {            string oPath = Server.MapPath(outPath);            bmp.Save(oPath, System.Drawing.Imaging.ImageFormat.Jpeg);             FileStream fs = new FileStream(oPath, FileMode.Open, FileAccess.Read);            byte[] imgData = new byte[(int)fs.Length];            fs.Read(imgData, 0, (int)fs.Length);             Response.BinaryWrite(imgData);            Response.ContentType = "image/pjpeg";            Response.End();            //bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);        }        catch (Exception ex)        {            Response.Write(ex.Message);        }        finally        {            image.Dispose();            bmp.Dispose();            g.Dispose();        }     }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.