No 61 · 缩放图片

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging ;
using System.IO ;

public partial class _Default : System.Web.UI.Page 
{
    public Size s = new Size(450, 693);//定义一个Size类型对象s,用于存储矩形的宽度和高度
    
    protected void Page_Load(object sender, EventArgs e)
    {
        imgPhoto.ImageUrl = "~/photo.aspx?src=photo.jpg&width=" + s.Width.ToString() + "&height=" + s.Height.ToString();
    
    }
    protected void btnSmall_Click(object sender, EventArgs e)
    {
        s = NewthumbSize(s.Width,s.Height);
        imgPhoto.ImageUrl = "~/photo.aspx?src=photo.jpg&width=" + s.Width.ToString() + "&height=" + s.Height.ToString();
    }
    //还原
    protected void btnShow_Click(object sender, EventArgs e)
    {
        imgPhoto.ImageUrl = "~/photo.aspx?src=photo.jpg&width="+s.Width .ToString ()+"&height="+s.Height.ToString ();
    }

    //改变图像高度和宽度
    public Size NewthumbSize(double currentwidth, double currentheight)
    {
        double width = Convert.ToDouble(txtWidth.Text)/100;
        double height = Convert.ToDouble(txtHeight.Text) / 100;
        Size NewSize = new Size(Convert.ToInt32(currentwidth * width), Convert.ToInt32(currentheight * height));
        return NewSize;
    }
}


【photo.aspx】

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class photo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        sendFile(Server.MapPath(Request.QueryString["src"]), Convert.ToInt32(Request.QueryString["width"]), Convert.ToInt32(Request.QueryString["height"]));
    }
    public void sendFile(string src, int width, int height)
    {
        System.Drawing.Image g = System.Drawing.Image.FromFile(src);
        ImageFormat thisFormat = g.RawFormat;
        Bitmap imgOutput = new Bitmap(g, width, height);
        if (thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
        {
            Response.ContentType = "image/gif";
        }
        else
        {
            Response.ContentType = "image/jpeg";
        }
        imgOutput.Save(Response.OutputStream, thisFormat);
        g.Dispose();
        imgOutput.Dispose();
    }
}


 

 

 

你可能感兴趣的:(object,String,存储,Class)