ASP.NET 生成缩略图

以下代码根据生成图片的宽、高,生成四种模式的图片缩略图

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();

        } 



    }

}

你可能感兴趣的:(asp.net)