Thành viên | Nội dung |
aspnet
 Lập trình không biên giới 600 bài
| 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(); } }
|
aspnet
 Lập trình không biên giới 600 bài
| ví dụ file ảnh là
abc.jpg
bây giờ resize chúng ta chỉ cần gọi
/photo.aspx/abc.jpg/600
là nó giãn ra theo độ rộng 600.
Còn nếu
/photo.aspx/abc.jpg/600/400
thì ép cả chiều rộng và cao
   --- Coding for food http://yenbai.awas.vn http://tknd.vn
|
 |