图片显示时加水印(不改变原图片)

[转]:http://adyhpq.blog.163.com/blog/

刚刚写了一个在显示图片是加水印的程序(不改变原图片)的程序,写出来和大家分享一下,也许有的人已经早就会了

原理很简单,就是拦截HTTP请求,写一个HttpHandler,首先我在web.config里面配置了图片的根路径:

<appSettings>

    <add key="monitorPath" value="pictures"/>

 </appSettings>

这个pictures是放所有图片的地方,包括下面的文件夹,从这里显示的图片都要加上水印.

然后写一个类,继承HttpHandler,这个类将自动放在AppCode下面,如下:

public class ShuiyinHandler:IHttpHandler

{

    public bool IsReusable

    {

        get { return true; }

    }

    public void ProcessRequest(HttpContext context)

    {

        try

        {

            //得到请求路径

            string url = context.Request.Url.AbsoluteUri.ToLower();

            string monitorPath = ConfigurationManager.AppSettings["monitorPath"];

            //是否包含图片路径

            bool IsInterestUrl = url.Contains(monitorPath);

            System.Drawing.Image imgSource = null;

           

              //判断原图片是否存在

            string physicalPath = context.Request.PhysicalPath;

            if (!System.IO.File.Exists(physicalPath))

            {

                context.Response.Write("图片不存在");

                return;

            }

            //如果不是要加水印的文件或文件夹,就原样输出

            if (!IsInterestUrl)

            {

                imgSource = System.Drawing.Image.FromFile(physicalPath);

                imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);

                imgSource.Dispose();

                return;

            }

            imgSource = System.Drawing.Image.FromFile(physicalPath);

            //判断是否是索引图像格式

            if (imgSource.PixelFormat == PixelFormat.Format1bppIndexed || imgSource.PixelFormat == PixelFormat.Format4bppIndexed || imgSource.PixelFormat == PixelFormat.Format8bppIndexed)

            {

                //转成位图,这步很重要

                Bitmap bitmap = new Bitmap(imgSource.Width, imgSource.Height);

                System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);

                System.Drawing.Font font = new System.Drawing.Font("Arial Black", 30.0f, System.Drawing.FontStyle.Bold);

                //将原图画在位图上

                graphic.DrawImage(imgSource, new Point(0, 0));

                //将水印加在位图上

                graphic.DrawString("www.dgxyt.com", font, System.Drawing.Brushes.Red, new System.Drawing.PointF());

                    //将位图输入到流

                bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);

                graphic.Dispose();

                imgSource.Dispose();

                bitmap.Dispose();

            }

            else

            {

                //不是索引图像格式,直接在上面加上水印

                System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(imgSource);

                System.Drawing.Font font = new System.Drawing.Font("Arial Black", 30.0f, System.Drawing.FontStyle.Bold);

                graphic.DrawString("www.dgxyt.com", font, System.Drawing.Brushes.Red, new System.Drawing.PointF());

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

                imgSource.Dispose();

                graphic.Dispose();

            }

            //标明类型为jpg:如果不标注,IE没有问题,但Firefox会出现乱码

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

            context.Response.Flush();

            context.Response.End();

        }

        catch

        {

            throw;

        }

    }

}

 

这样HttpHandler写好了,在web.config中中的HttpHandler节点下,加下如下代码:

<httpHandlers>

      <add verb="*" path="*.jpg,*.bmp,*.ico,*.jpeg" type="ShuiyinHandler"/>

  </httpHandlers>

 

这表示,jpg,bmp,ico,jpeg都可以加上水印,gif没有试过,不过大家可以试下.

 

写好之后,放在IIS上,居然不出来加水印的效果,这让我郁闷了好久,后来终于找到原因,原来是没有在IIS上没有注册以jpg,bmp,ico,jpeg为后缀的文件,IIS默认是不处理这些文件的。

 

处理方法:在网站上目录上右击,选择属性,选择主目录,配置,

随便选择列表中的一行,点击编缉,复制执行文件的路径,然后关闭,再点击添加,可执行文件用粘贴就可以了,后缀名写上.jpg,点击确定。按此方法再添加其他后缀名。

 

这样就实现水印效果了。

原文地址:https://www.cnblogs.com/sainaxingxing/p/1283303.html