01.
<%@ Page Language=
"C#"
Debug=
"true"
%>
02.
<%@ Import Namespace=
"System.Drawing"
%>
03.
<%@ Import Namespace=
"System.Drawing.Imaging"
%>
04.
05.
<script runat=
"server"
>
06.
void
btnUpload_OnClick(
object
sender, EventArgs e)
07.
{
08.
09.
if
(
this
.fiUpload.HasFile) {
10.
11.
int
intWidth = 0;
12.
int
intHeight = 0;
13.
string
UlFileName =
null
;
14.
string
NewFileName =
null
;
15.
16.
intWidth = 100;
17.
18.
19.
intHeight = 120;
20.
21.
UlFileName =
"MyImages/"
+ fiUpload.FileName;
22.
23.
24.
this
.fiUpload.SaveAs(Server.MapPath(UlFileName));
25.
26.
NewFileName =
"MyImages/Thumbnail_"
+ fiUpload.FileName;
27.
28.
29.
System.Drawing.Image objGraphic = System.Drawing.Image.FromFile(Server.MapPath(UlFileName));
30.
31.
Bitmap objBitmap =
default
(Bitmap);
32.
33.
if
(intHeight > 0) {
34.
objBitmap =
new
Bitmap(objGraphic, intWidth, intHeight);
35.
}
36.
else
{
37.
if
(objGraphic.Width > intWidth) {
38.
double
ratio = objGraphic.Height / objGraphic.Width;
39.
intHeight = (
int
)ratio * (
int
)intWidth;
40.
objBitmap =
new
Bitmap(objGraphic, intWidth, intHeight);
41.
}
42.
else
{
43.
objBitmap =
new
Bitmap(objGraphic);
44.
}
45.
}
46.
47.
48.
objBitmap.Save(Server.MapPath(NewFileName.ToString()), objGraphic.RawFormat);
49.
50.
51.
objGraphic.Dispose();
52.
53.
54.
this
.imgPicture.Visible =
true
;
55.
this
.imgPicture.ImageUrl = NewFileName;
56.
}
57.
}
58.
</script>
59.
<html>
60.
<head>
61.
<title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title>
62.
</head>
63.
<body>
64.
<form id=
"form1"
runat=
"server"
>
65.
<asp:FileUpload id=
"fiUpload"
runat=
"server"
></asp:FileUpload>
66.
<input id=
"btnUpload"
type=
"button"
OnServerClick=
"btnUpload_OnClick"
value=
"Upload"
runat=
"server"
/>
67.
<hr />
68.
<asp:Image id=
"imgPicture"
Visible=
"false"
runat=
"server"
/><br /><br />
69.
</form>
70.
</body>
71.
</html>
72.
</form>