กรณีแรก ทำ thumbnail รูปภาพ โดยการลดขนาดรูปแล้วอัพโหลดเก็บลงที่ server จากนั้นค่อยให้เว็บเพจเราเรียกออกมาใช้จาก server ของเราเอง
Code (VB.NET)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
Sub btnUpload_OnClick(sender As Object, e As EventArgs)
If Me.fiUpload.HasFile Then
Dim intWidth,intHeight As Integer
Dim UlFileName,NewFileName As String
intWidth = 100 '*** Fix Width ***'
'intHeight = 0 '*** If = 0 Auto Re-Cal Size ***'
intHeight = 120
UlFileName = "MyImages/" & fiUpload.FileName
'*** Save Images ***'
Me.fiUpload.SaveAs(Server.MapPath(UlFileName))
NewFileName = "MyImages/Thumbnail_" & fiUpload.FileName
Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(UlFileName))
Dim objBitmap As Bitmap
'*** Calculate Height ***'
If intHeight > 0 Then
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
Else
If objGraphic.Width > intWidth Then
Dim ratio As Double = objGraphic.Height / objGraphic.Width
intHeight = ratio * intWidth
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
Else
objBitmap = New Bitmap(objGraphic)
End If
End If
'*** Save As ***'
objBitmap.Save(Server.MapPath(NewFileName), objGraphic.RawFormat)
'*** Close ***'
objGraphic.Dispose()
'*** View Images ***'
Me.imgPicture.Visible = True
Me.imgPicture.ImageURL = NewFileName
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload>
<input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" />
<hr />
<asp:Image id="imgPicture" Visible="false" runat="server" /><br /><br />
</form>
</body>
</html>
</form>