码迷,mamicode.com
首页 > Windows程序 > 详细

C# 对图片加水印

时间:2015-01-17 16:27:24      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

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

        }
    }
}

 

C# 对图片加水印

标签:

原文地址:http://www.cnblogs.com/zoro-zero/p/4230574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!