C# 对图片加水印
2024-10-15 18:05:56
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 > )
{
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,),new Rectangle(,,width,height)); //加上水印
g.DrawImage(logo,width--logo.Width,height--logo.Height); } bmp.Save(newFullname); bmp.Dispose();
logo.Dispose(); }
}
}
最新文章
- ConcurrentAsyncQueue 2014-09-07
- Android下集成Paypal支付
- 09-FZ6R 白色
- iOS多线程编程之NSThread的使用(转)
- about云资源共享
- jquery动态创建form并提交到.ashx文件处理
- HDOJ 2191
- 使用NUnit进行项目的单元测试
- HttpClient---------demo
- Jquery Mobile 记录
- 《JavaScript高级程序设计》读书笔记 ---基本包装类型
- [转载] Redis-benchmark使用总结
- win10下 github+hexo搭建个人博客.md
- scala的多种集合的使用(7)之集Set的操作方法
- 自适应rem.js
- mysql5.6 主从同步配置
- mvn打包到私服的命令
- TDSQL“相似查询工具MSQL+”入选VLDB论文
- January 01st, 2018 Week 01st Monday
- Windows 10官方原版ISO制作方法
热门文章
- C#.字符串转数组,数组转字符串
- pgm12
- FieldGroup绑定ItemDataSource
- linux上搭建nginx
- Change upload file limit in specified Webapllication in SharePoint (PowerShell)
- 【codeforces 103E】 Buying Sets
- android关闭日志
- AtCoder Grand Contest 010 D - Decrementing
- 1: Myeclipse10 优化设置
- hdu 4685(强连通分量+二分图的完美匹配)