ASP.NET Upload Resize Insert to Database |
ASP.NET Upload & Resize Image เป็นการสร้าง Form Upload และ Resize รูปลงใน Database โดยทำการเก็บรูปออกเป็น 2 รูปคือ รูปที่เป็นต้นฉบับ และรูปที่เป็น Thumbnail
Language Code : VB.NET || C#
Framework : 2,3,4
AspNetUploadResizeDB1.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
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()
'*** Nothing ***'
objBitmap = Nothing
objGraphic = Nothing
'*** Save to Database ***'
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strConnString,strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
strSQL = "INSERT INTO images (ImagesName,Thumbnails) " & _
"VALUES ('" & UlFileName & "','" & NewFileName & "')"
objCmd = New OleDbCommand()
With objCmd
.Connection = objConn
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()
objCmd = Nothing
objConn.Close()
objConn = Nothing
'*** Save to Database (End) ***'
'*** View Images ***'
Me.imgPicture.Visible = True
Me.imgPicture.ImageURL = NewFileName
'*** Hyperlink ***'
Me.hplLink1.Visible = True
Me.hplLink1.NavigateUrl = "AspNetUploadResizeDB2.aspx"
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 />
<asp:HyperLink id="hplLink1" Visible = "false" runat="server">View files</asp:HyperLink>
</form>
</body>
</html>
</form>
AspNetUploadResizeDB2.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
BindData()
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM images"
Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to DataList ***'
myDataList.DataSource = dtReader
myDataList.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DataList</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList id="myDataList" RepeatColumns="2" runat="server">
<HeaderTemplate>
<b>My Girl</b>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100px" align="center">
<a href="<%#Container.DataItem("ImagesName")%>" target="blank">
<img src="<%#Container.DataItem("Thumbnails")%>" border="0">
</a>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
<asp:Label id="xxx" runat="server"/>
</form>
</body>
</html>
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-10-26 20:20:16 /
2017-03-29 11:30:30 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|