图片水印

来源:互联网 发布:计算瓷砖用量软件 编辑:程序博客网 时间:2024/04/28 03:34

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Drawing;
using System.Text;

namespace xhyBookShop
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ImgHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string path = context.Server.MapPath("~/BookPic/");
            string fileSrc = path + context.Request["src"];
            string defSrc = path + "default.jpg";
            string waterSrc = path + "sign.jpg";

            Image pic = null;
            //创建站点图片
            context.Response.ContentType = "image/jpg";
            Image waterPic = Image.FromFile(waterSrc);
            //创建水印绘图对象
            Graphics wg = Graphics.FromImage(waterPic);
            //绘制
            wg.DrawString(
                "title",
                new Font("粗体", 15, FontStyle.Bold),
                Brushes.Black,
                new Point(20, 12));
            wg.Dispose();
            if (File.Exists(fileSrc))
            {
                //设置输出类型
                context.Response.ContentType = "image/jpg";
                //创建图片
                pic = Image.FromFile(fileSrc);
                //绘制
                Graphics g = Graphics.FromImage(pic);
                g.DrawImage(waterPic, 0, 0);
                g.Dispose();
            }
            else
            {
                //设置输出类型
                context.Response.ContentType = "image/jpg";
                //创建图片
                pic = Image.FromFile(defSrc);
                //创建绘图对象
                Graphics g = Graphics.FromImage(pic);
                //绘制
                g.DrawImage(waterPic, 0, 0);
                //g.DrawString(
                //    "title",
                //    new Font("粗体", 15, FontStyle.Bold),
                //    Brushes.Black,
                //    new Point(0, 0));
                g.DrawString(
                    "暂无图片",
                    new Font("粗体", 30, FontStyle.Bold),
                    Brushes.Gray,
                    new Point(0, 60));
            }
            context.Response.ContentType = "image/jpeg";
            pic.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            pic.Dispose();

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

原创粉丝点击