給圖片加水印

    編寫一個ConvertImage.cs類

 

    using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

/// <summary>
/// ConvertImage 的摘要說明
/// </summary>
public class ConvertImage:IHttpHandler
{
    public ConvertImage()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }

    #region IHttpHandler 成員

    public bool IsReusable
    {
        get { return false ; }
    }

    public void ProcessRequest(HttpContext context)
    {
        System.Drawing.Image img = null;
        if (File.Exists(context.Request.PhysicalPath))
        {
            string path = context.Request.PhysicalPath;

            img = System.Drawing.Image.FromFile(path);

            System.Drawing.Image backImg = System.Drawing.Image.FromFile(context.Server.MapPath("~/Images/1.jpg"));

            Graphics g = Graphics.FromImage(img);

            g.DrawImage(backImg, new Rectangle(img.Width - backImg.Width, img.Height - backImg.Height, backImg.Width, backImg.Height), 0, 0, backImg.Width, backImg.Height, System.Drawing.GraphicsUnit.Pixel);

            g.Dispose();

            backImg.Dispose();

        }
        else
        {
            img = System.Drawing.Image.FromFile(context.Request.MapPath("~/Images/default.jpg"));
        }

        context.Response.ContentType = "image/jpeg";

        img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        img.Dispose();

        context.Response.End();


    }

    #endregion
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章