(C#) ASP.NET Upload Resize Insert to Database |
(C#) ASP.NET Upload & Resize Image เป็นการสร้าง Form Upload และ Resize รูปลงใน Database โดยทำการเก็บรูปออกเป็น 2 รูปคือ รูปที่เป็นต้นฉบับ และรูปที่เป็น Thumbnail
Language Code : VB.NET || C#
Framework : 2,3,4
AspNetUploadResizeDB1.aspx
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
public void btnUpload_OnClick(object sender, EventArgs e)
{
if (this.fiUpload.HasFile) {
int intWidth = 0;
int intHeight = 0;
string UlFileName = null;
string NewFileName = null;
intWidth = 100;
//*** Fix Width ***//
//intHeight = 0 //*** If = 0 Auto Re-Cal Size ***//
intHeight = 120;
UlFileName = "MyImages/" + fiUpload.FileName;
//*** Save Images ***//
this.fiUpload.SaveAs(Server.MapPath(UlFileName));
NewFileName = "MyImages/Thumbnail_" + fiUpload.FileName;
System.Drawing.Image objGraphic = System.Drawing.Image.FromFile(Server.MapPath(UlFileName));
Bitmap objBitmap = default(Bitmap);
//*** Calculate Height ***//
if (intHeight > 0) {
objBitmap = new Bitmap(objGraphic, intWidth, intHeight);
}
else {
if (objGraphic.Width > intWidth) {
double ratio = objGraphic.Height / objGraphic.Width;
intHeight = (int)ratio * (int)intWidth;
objBitmap = new Bitmap(objGraphic, intWidth, intHeight);
}
else {
objBitmap = new Bitmap(objGraphic);
}
}
//*** Save As ***//
objBitmap.Save(Server.MapPath(NewFileName), objGraphic.RawFormat);
//*** Close ***//
objGraphic.Dispose();
//*** Nothing ***//
objBitmap = null;
objGraphic = null;
//*** Save to Database ***//
OleDbConnection objConn = default(OleDbConnection);
OleDbCommand objCmd = default(OleDbCommand);
string strConnString = null;
string strSQL = null;
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();
{
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
}
objCmd.ExecuteNonQuery();
objCmd = null;
objConn.Close();
objConn = null;
//*** Save to Database (End) ***//
//*** View Images ***//
this.imgPicture.Visible = true;
this.imgPicture.ImageUrl = NewFileName;
//*** Hyperlink ***//
this.hplLink1.Visible = true;
this.hplLink1.NavigateUrl = "AspNetUploadResizeDB2.aspx";
}
}
</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="C#" Debug="true" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
public void Page_Load(object sender, EventArgs e)
{
string strConnString = null;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
BindData();
}
public void BindData()
{
string strSQL = null;
strSQL = "SELECT * FROM images";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataList ***//
myDataList.DataSource = dtReader;
myDataList.DataBind();
dtReader.Close();
dtReader = null;
}
public void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
</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="<%# DataBinder.Eval(Container.DataItem, "ImagesName") %>" target="blank">
<img src="<%# DataBinder.Eval(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 |
|
|
|
|
|
|
|