PDA

View Full Version : TẠO THUMBNAIL IMAGE (ASP.NET)


SHEVA-DIEUTHUYEN
24-05-2004, 21:48
Tạo thumbnail image (asp.net)


Chúng tôi xin giới thiệu với các bạn một cách làm thumbnail image. Đoạn code sau còn tự tạo ra 1 ảnh nhỏ, rất tiện dụng làm picture gallary.

'------ 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 LHQ 2003'') ' 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ũ
--------------------------------------------------------------------
'cách mở,sửa rồi save lại 1 file bất kỳ trực tiếp trên internet mà ko cần 'down về sửa rồi lại up lên net

function opentoedit(byval fn as string) as string
if trim(fn)<>'''' then
try
dim s as New StreamReader(server.mappath(fn),Encoding.UTF8)
Dim text as String = s.Readtoend()
result=text
s.Close()
catch objerror as exception
response.write(''<center>Can not open that file,plzz trying with other file</center>'')
end try
end if
End function

function save(byval fn as string,byval text as string) as string
dim sw as new streamwriter(server.mappath(fn))
sw.write(text.tochararray,0,text.length)
sw.close
End Sub

--------------------------------------
'file dùng để upload lên cùng thư mục chứa file này

<%@ Page Language=''VB'' %>

<%@ import Namespace=''system.io'' %> '<--- nhập vào tên ko gian cần thiết sử lý file

<script runat=''server''>

Sub Button1_Click(sender As Object, e As EventArgs)

dim p as string = request.PhysicalApplicationPath '<---- lấy đường dẫn vật lý của thư mục ứng dụng

if not (f.postedfile is nothing) then

try

dim fi as fileinfo

fi=new fileinfo(f.postedfile.filename)

f.postedfile.saveas(p & ''\'' & fi.name)

response.write(''<center>Upload Completed'')

catch objerror as exception '<----- đón bắt lỗi

response.write(''<center>Upload Error'')

end try

end if

End Sub

Sub Page_Load(sender As Object, e As EventArgs)

server.scripttimeout=999999 '<---- set thời gian script có hiệu lực,chặn trường hợp có lỗi khi upload wá lâu

'để có thể upload file size > 4 mb(mặc định) thì sửa file web.config như sau

'<httpRuntime
'.............
'maxRequestLength=''4120'' <---- mb max qui định file có thể upload
'.............
'/>
End Sub
</script>
<html>
<head>
</head>
<body bgcolor=''silver''>
<form enctype=''multipart/form-data'' runat=''server''>
Upload one
file<input id=''f'' hidefocus=''hidefocus'' type=''file'' runat=''server'' />
<asp:Button id=''Button2'' onclick=''Button1_Click'' runat=''server'' Text=''Upload''></asp:Button>
</form>
</body>
</html>

SƯU TẦM