PDA

View Full Version : images resize trong asp.net



tuanhaikh
23-11-2010, 21:07
Mình đang nghiên cứu chổ images resize (khi upload mình hình ảnh lên mạng sẻ xử dụng 1 đoạn code để thây đổi kích thước ảnh về kích thước mình mong muốn sau đó chép hình và lưu xuống csdl). Mình có lướt trên mạng thấy bài viết này nhưng chưa biết cách dùng như thế nào.

CÁc bạn xem rồi góp ý nhé

'------ tạo file img.aspx để lấy hình nhỏ---

<%@ Page Language=''VB'' %>
<%@ import Namespace=''System.Drawing'' %>
<%@ import Namespace=''System.io'' %>
<%@ import Namespace=''System.Drawing.Imaging'' %>
<script runat=''server''>

function wh(byval old as point,thu as point) as point
if old.x> old.y then
wh.x=thu.x
wh.y=(old.y / old.x)*thu.y
end if
if old.x < old.y then
wh.y=thu.y
wh.x=(old.x / old.y)*thu.x
end if
if old.x= old.y then
wh.x=thu.x
wh.y=thu.y
end if
end function


function thumb(filename as string,byval s as integer) as bitmap
Dim b As System.Drawing.image=System.Drawing.image.FromFile (server.mappath(filename))
dim c as new bitmap(s,s)
dim p as point=wh(new point(b.width,b.height),new point(s,s))
c=b.GetThumbnailImage(p.x,p.y,nothing,nothing)
return c
b.dispose
c.dispose

end function
Sub Page_Load(sender As Object, e As EventArgs)
dim name as string=server.urldecode(request(''name''))
if name<>nothing then thumb(name,100).save (response.outputstream,imageformat.jpeg) else response.write(''thumbnail file by Panh'') ' số 100 là kich thước ảnh lúc thumb
End Sub

</script>
<html>
<head>
</head>
<body bgcolor=''silver''>
<form runat=''server''>
</form>
</body>
</html>

ví dụ muốn tạo thumb ảnh ''loveyou.jpg'' thì ghi như sau <img src=img.aspx?name=loveyou.jpg> ---> tạo ra ảnh nhỏ mà nhẹ,nhanh . Tỉ lệ kich thứơc width, height như cũ