<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.IO"%> <script runat="server"> void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.txtFrom.Text = "Gallery/"; this.txtTo.Text = "Resize/"; } } void btnCreate_OnClick(object sender, EventArgs e) { DirectoryInfo myDirInfo; string FileName; string NewFileName; this.lblText.Text = ""; myDirInfo = new DirectoryInfo(Server.MapPath(this.txtFrom.Text)); ? ? FileInfo[] arrFileInfo = myDirInfo.GetFiles("*.jpg"); foreach (FileInfo myFileInfo in arrFileInfo) { FileName = txtFrom.Text + myFileInfo.Name; NewFileName = txtTo.Text + "Thumbnail_" + myFileInfo.Name; //*** Call to function resize ***// ResizeImages(FileName, NewFileName); this.lblText.Text = this.lblText.Text + (FileName + "===> <a href=" + NewFileName + ">" + NewFileName + "</a><br>"); } } 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:Label id="lblForm" runat="server" Text="Path From" Width="58px"></asp:Label> <asp:Textbox id="txtFrom" runat="server"></asp:Textbox><br /> <asp:Label id="lblTo" runat="server" Text="Path To" Width="58px"></asp:Label> <asp:Textbox id="txtTo" runat="server"></asp:Textbox> <input id="btnCreate" type="button" OnServerClick="btnCreate_OnClick" value="Create" runat="server" /> <hr /> <asp:Label id="lblText" runat="server"></asp:Label> </form> </body> </html> </form>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท