Diễn đàn    ASP.NET & Sharepoint MOSS, WSS 2007    Resize một cái ảnh dành cho việc trình bày trang tin

Thành viênTrả lời
aspnet

Lập trình không biên giới
608  bài
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();
}
}
 
aspnet

Lập trình không biên giới
608  bài
11-3-2009 4:24:1
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
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM