11-3-2009 4:21:51
private int wid = 0;
private int hei = 0;
private string code = "";
private void Parsing()
{
string p = Request.Path;
string[] s = p.Split('/');
if (s.Length > 2) code = s[2];
if (s.Length > 3) wid = Convert.ToInt32(s[3]);
if (s.Length > 4) hei = Convert.ToInt32(s[4]);
}
private string GetFile()
{
if (code != "")
{
string[] f = System.IO.Directory.GetFiles(Server.MapPath("/"), code + "*");
if (f.Length > 0) return f[0];
}
return "";
}
private void CaculationSize(int imgWid, int imgHei)
{
if (wid == 0) wid = imgWid;
if (hei == 0)
{
if (imgWid == wid) hei = imgHei;
hei = imgHei * wid / imgWid;
}
}
protected void Page_Load(object sender, EventArgs e)
{
Parsing();
string file = GetFile();
if (file == "") return;
if (System.IO.File.Exists(file))
{
System.Drawing.Image img = System.Drawing.Image.FromFile(file);
CaculationSize(img.Width, img.Height);
Bitmap bmp = new Bitmap(wid, hei);
Graphics gr = Graphics.FromImage(bmp);
gr.DrawImage(img, 0, 0, wid, hei);
Response.ContentType = "image/jpeg";
Response.Clear();
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
Response.Flush();
Response.End();
}
}