using System; using System.Collections; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.IO; using System.Drawing; namespace WebAppTest { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; HttpPostedFile hpf = context.Request.Files["upload"]; if (hpf.ContentLength > 0) { string path = context.Server.MapPath("~/resource/image/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = hpf.FileName; string newFilename = DateTime.Now.ToFileTime() + Path.GetExtension(filename); string fullname = path + newFilename; hpf.SaveAs(fullname); string waterMarkFileName = context.Server.MapPath("~/resource/image/logo.png"); AddWaterMark(fullname, waterMarkFileName); context.Response.Write("upload success"); } else { context.Response.Write("select file"); } } public bool IsReusable { get { return false; } } /// <summary> /// 加上水印 /// </summary> /// <param name="fullname">需要加上水印图片的全路径</param> /// <param name="waterMarkFileName">水印图片的全路径</param> public void AddWaterMark(string fullname,string waterMarkFileName) { //D:\projects\VS2008\WebAppTest\WebAppTest\resource\image 结尾没有‘\‘ string dicName = Path.GetDirectoryName(fullname); string newFilename = DateTime.Now.ToFileTime() + Path.GetExtension(fullname); string newFullname = dicName + "/" + newFilename; Bitmap bmp = new Bitmap(fullname); int width = bmp.Width; int height = bmp.Height; Bitmap logo = new Bitmap(waterMarkFileName); //设置透明 logo.MakeTransparent(); using(Graphics g = Graphics.FromImage(bmp)) { //将图片加上边框 g.DrawRectangle(new Pen(Color.Green,6),new Rectangle(0,0,width,height)); //加上水印 g.DrawImage(logo,width-6-logo.Width,height-6-logo.Height); } bmp.Save(newFullname); bmp.Dispose(); logo.Dispose(); } } }
时间: 2024-10-09 05:46:38