Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > C# (.NET) > ASP.NET Image > (C#) ASP.NET Upload Resize Insert to Database



Clound SSD Virtual Server

(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

ASP.NET Image

ASP.NET Image

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-26 20:20:16 / 2017-03-29 11:30:30
  Download : Download  (C#) ASP.NET Upload Resize Insert to Database
 Sponsored Links / Related

 
(C#) ASP.NET Create Image
Rating :

 
(C#) ASP.NET Write Text to Image
Rating :

 
(C#) ASP.NET Write Text to Picture
Rating :

 
(C#) ASP.NET (Watermark) Upload and Write Text to Picture
Rating :

 
(C#) ASP.NET Add Logo/CopyRight to Image
Rating :

 
(C#) ASP.NET Upload and Add Logo/CopyRight to Image
Rating :

 
(C#) ASP.NET Resize Image
Rating :

 
(C#) ASP.NET Create Thumbnail Image
Rating :

 
(C#) ASP.NET Upload & Resize Image
Rating :

 
(C#) ASP.NET Upload Resize Insert to Database
Rating :

 
(C#) ASP.NET Multiple Upload & Multiple Resize
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่