C# 对图片加水印

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();

        }
    }
}

 

转载于:https://www.cnblogs.com/zoro-zero/p/4230574.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值