<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <script runat="server"> 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.ToString()), objGraphic.RawFormat); //*** Close ***// objGraphic.Dispose(); //*** View Images ***// this.imgPicture.Visible = true; this.imgPicture.ImageUrl = NewFileName; } } </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 /> </form> </body> </html> </form>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท