public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //1:接收发送过来的文件的数据. HttpPostedFile file =context.Request.Files["Filedata"]; string fileName = Path.GetFileName(file.FileName);//文件名称 string fileExt = Path.GetExtension(fileName).ToLower();//扩展名. string waterFile = context.Server.MapPath("/Images/error.jpg");//用来做水印的图片 if (fileExt == ".jpg") { //创建不同的文件夹保存上传的文件. string dir = "/ImageUpload/"+DateTime.Now.Year+"/"+DateTime.Now.Month+"/"+DateTime.Now.Day+"/"; Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(dir))); //要对上传的文件进行重命名.(file.InputStream:获取上传文件的文件流) string fullDir = dir +Common.WebCommon.GetStreamMD5(file.InputStream)+ fileExt; using (Image img = Image.FromStream(file.InputStream))//指定上传的图片 { using (Bitmap map = new Bitmap(img.Width, img.Height))//创建画布 { using (Graphics g = Graphics.FromImage(map))//为画布创建画笔. { using (Image waterImage = Image.FromFile(waterFile)) { g.DrawImage(img, new Point(0,0));//将上传的图片画在了画布上. //在大图上画水印图片,第二个参数;在什么位置开始画,画多么大。 //后面:画哪一部分. g.DrawImage(waterImage, new Rectangle(img.Width - waterImage.Width, img.Height - waterImage.Height, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, GraphicsUnit.Pixel, SetImageAttr(50));//使用调整后的图片的颜色或透明度信息开始画图片. map.Save(context.Server.MapPath(fullDir));//保存图片 context.Response.Write("ok:" + fullDir); } } } } //file.SaveAs(context.Server.MapPath(fullDir)); //context.Response.Write("ok:"+fullDir); //可以缩略图. //file.SaveAs(context.Server.MapPath("/ImageUpload/"+fileName)); //context.Response.Write("ok:/ImageUpload/" + fileName); } } public ImageAttributes SetImageAttr(float ff) { //定义矩阵:RGBAW float[][] f = { new float[]{ff/100f,0,0,0,0}, new float[]{0,1,0,0,0}, new float[]{0,0,1,0,0}, new float[]{0,0,0,1,0}, new float[]{0,0,0,0,1} }; ColorMatrix colorMatrix = new ColorMatrix(f);//使用自己定义的矩阵。 ImageAttributes imageAttr = new ImageAttributes();//指定了图像的颜色的信息 imageAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);//用我们自己定义的矩阵跳转图片的颜色信息。 return imageAttr; }
时间: 2024-10-10 03:50:20