<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { int intWidth = 0; int intHeight = 0; string FileName; string NewFileName; intWidth = 100; //*** Fix Width ***// intHeight = 0; //*** If = 0 Auto Re-Cal Size ***// FileName = "MyImages/Mygirl.jpg"; NewFileName = "MyImages/New_Mygirl.jpg"; System.Drawing.Image objGraphic = System.Drawing.Image.FromFile(Server.MapPath(FileName)); 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; //*** View Images ***// this.imgPicture.ImageUrl = NewFileName; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title> </head> <body> <form id="form1" runat="server"> <asp:Image id="imgPicture" runat="server" /> </form> </body> </html> </form>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท