01.
<%@ Page Language=
"VB"
%>
02.
<%@ Import
Namespace
=
"System.Drawing"
%>
03.
<%@ Import
Namespace
=
"System.Drawing.Imaging"
%>
04.
<%@ Import
Namespace
=
"System.IO"
%>
05.
<script runat=
"server"
>
06.
07.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
08.
If
Not
Page.IsPostBack()
Then
09.
Me
.txtFrom.Text =
"Gallery/"
10.
Me
.txtTo.Text =
"Resize/"
11.
End
IF
12.
End
Sub
13.
14.
Sub
btnCreate_OnClick(sender
As
Object
, e
As
EventArgs)
15.
16.
Dim
myDirInfo
As
DirectoryInfo
17.
Dim
arrFileInfo
As
Array
18.
Dim
myFileInfo
As
FileInfo
19.
Dim
FileName
As
String
20.
Dim
NewFileName
As
String
21.
22.
Me
.lblText.Text =
""
23.
24.
myDirInfo =
New
DirectoryInfo(Server.MapPath(
Me
.txtFrom.Text))
25.
arrFileInfo = myDirInfo.GetFiles(
"*.jpg"
)
26.
For
Each
myFileInfo
In
arrFileInfo
27.
FileName = txtFrom.Text & myFileInfo.Name
28.
NewFileName = txtTo.Text &
"Thumbnail_"
&myFileInfo.Name
29.
30.
31.
Call
ResizeImages(FileName,NewFileName)
32.
33.
Me
.lblText.Text =
Me
.lblText.Text & (FileName &
"===> <a href="
& NewFileName &
">"
& NewFileName &
"</a><br>"
)
34.
Next
myFileInfo
35.
36.
End
Sub
37.
38.
Sub
ResizeImages(FileName,NewFileName)
39.
Dim
intWidth,intHeight
As
Integer
40.
41.
intWidth = 100
42.
intHeight = 0
43.
44.
Dim
objGraphic
As
System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(FileName))
45.
46.
Dim
objBitmap
As
Bitmap
47.
48.
If
intHeight > 0
Then
49.
objBitmap =
New
Bitmap(objGraphic, intWidth, intHeight)
50.
Else
51.
If
objGraphic.Width > intWidth
Then
52.
Dim
ratio
As
Double
= objGraphic.Height / objGraphic.Width
53.
intHeight = ratio * intWidth
54.
objBitmap =
New
Bitmap(objGraphic, intWidth, intHeight)
55.
Else
56.
objBitmap =
New
Bitmap(objGraphic)
57.
End
If
58.
End
If
59.
60.
61.
objBitmap.Save(Server.MapPath(NewFileName), objGraphic.RawFormat)
62.
63.
64.
objGraphic.Dispose()
65.
66.
67.
objBitmap =
Nothing
68.
objGraphic =
Nothing
69.
70.
End
Sub
71.
72.
</script>
73.
<html>
74.
<head>
75.
<title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title>
76.
</head>
77.
<body>
78.
<form id=
"form1"
runat=
"server"
>
79.
<asp:Label id=
"lblForm"
runat=
"server"
Text=
"Path From"
Width=
"58px"
></asp:Label>
80.
<asp:Textbox id=
"txtFrom"
runat=
"server"
></asp:Textbox><br />
81.
<asp:Label id=
"lblTo"
runat=
"server"
Text=
"Path To"
Width=
"58px"
></asp:Label>
82.
<asp:Textbox id=
"txtTo"
runat=
"server"
></asp:Textbox>
83.
<input id=
"btnCreate"
type=
"button"
OnServerClick=
"btnCreate_OnClick"
value=
"Create"
runat=
"server"
/>
84.
<hr />
85.
<asp:Label id=
"lblText"
runat=
"server"
></asp:Label>
86.
</form>
87.
</body>
88.
</html>
89.
</form>