<%@ Page Language="C#" Debug="true"%> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <script runat="server"> void ddlNum_Changed(object sender, System.EventArgs e) { int i = 0; FileUpload fileUpload; Label lblBr; for (i = 1; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value); i++) { fileUpload = new FileUpload(); { fileUpload.ID = "fiUpload" + i; } lblBr = new Label(); lblBr.Text = "<br>"; this.pnlCtrl.Controls.Add(fileUpload); this.pnlCtrl.Controls.Add(lblBr); } if (Convert.ToInt32(this.ddlNum.SelectedItem.Value) > 0) { this.pnlUpload.Visible = true; } else { this.pnlUpload.Visible = false; this.form1.Enctype = "multipart/form-data"; } } void btnUpload_OnClick(object sender, EventArgs e) { int i = 0; HttpFileCollection myUpoad = Request.Files; HttpPostedFile myFiles; string FileName = null; string NewFileName = null; for (i = 0; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value) - 1; i++) { myFiles = myUpoad[i]; //*** Check Control ***// if (myUpoad.Keys[i].ToString() == ("fiUpload" + (i + 1)).ToString()) { if (!string.IsNullOrEmpty((myFiles.FileName))) { FileName = "MyUpload/" + System.IO.Path.GetFileName(myFiles.FileName); NewFileName = "MyUpload/Thumbnail_" + System.IO.Path.GetFileName(myFiles.FileName); //*** Upload Original Images ***// myFiles.SaveAs(Server.MapPath(FileName)); //*** Call to function resize ***// ResizeImages(FileName, NewFileName); this.lblText.Text = this.lblText.Text + System.IO.Path.GetFileName(myFiles.FileName) + " uploaded & resize.<br>"; } } } this.ddlNum.Visible = false; this.pnlUpload.Visible = false; } void ResizeImages(object FileName, object NewFileName) { int intWidth = 0; int intHeight = 0; intWidth = 100; //*** Fix Width ***// intHeight = 0; //*** If = 0 Auto Re-Cal Size ***// System.Drawing.Image objGraphic = System.Drawing.Image.FromFile(Server.MapPath(FileName.ToString())); Bitmap objBitmap; //*** 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.ToString()), objGraphic.RawFormat); //*** Close ***// objGraphic.Dispose(); //*** Nothing ***// objBitmap = null; objGraphic = null; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title> </head> <body> <form id="form1" runat="server"> <asp:DropDownList id="ddlNum" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNum_Changed"> <asp:ListItem Text="0" Value="0"></asp:ListItem> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> <asp:ListItem Text="4" Value="4"></asp:ListItem> <asp:ListItem Text="5" Value="5"></asp:ListItem> <asp:ListItem Text="6" Value="6"></asp:ListItem> <asp:ListItem Text="7" Value="7"></asp:ListItem> <asp:ListItem Text="8" Value="8"></asp:ListItem> </asp:DropDownList> <asp:Panel id="pnlCtrl" runat="server"></asp:Panel> <asp:Panel id="pnlUpload" runat="server" Visible="false"> <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" /> <hr /> <asp:Image id="imgPicture" Visible="false" runat="server" /><br /> </asp:Panel> <asp:Label id="lblText" runat="server"></asp:Label> </form> </body> </html> </form>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท