当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net如何在图片上加水印文字具体实现

asp.net如何在图片上加水印文字具体实现

2017年12月12日  | 移动技术网IT编程  | 我要评论

上海 房价,搜房网 青岛,cnblue直觉

第一步,添加一个一般处理程序(handler),本例是imagehandler

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.net.mime;
using system.io;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;

/// <summary>
/// summary description for imagehandler
/// </summary>
public class imagehandler : ihttphandler
{
    public imagehandler()
    {
    }

    public string getcontenttype(string path)
    {
        switch (path.getextension(path))
        {
            case ".bmp": return "image/bmp";
            case ".gif": return "image/gif";
            case ".jpg": return "image/jpeg";
            case ".png": return "image/png";
            default: break;
        }
        return string.empty;
    }

    public imageformat getimageformat(string path)
    {
        switch (path.getextension(path).tolower())
        {
            case ".bmp": return imageformat.bmp;
            case ".gif": return imageformat.gif;
            case ".jpg": return imageformat.jpeg;
            case ".png": return imageformat.png;
            default: return null;
        }
    }

    protected byte[] watermarkimage(httpcontext context)
    {

        byte[] imagebytes = null;
        if (file.exists(context.request.physicalpath))
        {
            // normally you'd put this in a config file somewhere.
            string watermark = "世复检测";

            image image = image.fromfile(context.request.physicalpath);

            graphics graphic;
            if (image.pixelformat != pixelformat.indexed && image.pixelformat != pixelformat.format8bppindexed && image.pixelformat != pixelformat.format4bppindexed && image.pixelformat != pixelformat.format1bppindexed)
            {
                // graphic is not a indexed (gif) image
                graphic = graphics.fromimage(image);
            }
            else
            {
                /* cannot create a graphics object from an indexed (gif) image.
                 * so we're going to copy the image into a new bitmap so
                 * we can work with it. */
                bitmap indexedimage = new bitmap(image);
                graphic = graphics.fromimage(indexedimage);

                // draw the contents of the original bitmap onto the new bitmap.
                graphic.drawimage(image, 0, 0, image.width, image.height);
                image = indexedimage;
            }
            graphic.smoothingmode = smoothingmode.antialias & smoothingmode.highquality;

            font myfont = new font("arial", 15);
            solidbrush brush = new solidbrush(color.fromargb(255, color.red));

            /* this gets the size of the graphic so we can determine
             * the loop counts and placement of the watermarked text. */
            sizef textsize = graphic.measurestring(watermark, myfont);

            //// write the text across the image.
            //for (int y = 0; y < image.height; y++)
            //{
            //    for (int x = 0; x < image.width; x++)
            //    {
            //        pointf pointf = new pointf(x, y);
            //        graphic.drawstring(watermark, myfont, brush, pointf);
            //        x += convert.toint32(textsize.width);
            //    }
            //    y += convert.toint32(textsize.height);
            //}


            // write the text at the right bottom of the image.
            for (int y = image.height-25; y < image.height; y++)
            {
                for (int x = image.width-100; x < image.width; x++)
                {
                    pointf pointf = new pointf(x, y);
                    graphic.drawstring(watermark, myfont, brush, pointf);
                    x += convert.toint32(textsize.width);
                }
                y += convert.toint32(textsize.height);
            }

            using (memorystream memorystream = new memorystream())
            {
                image.save(memorystream, getimageformat(context.request.physicalpath));
                imagebytes = memorystream.toarray();
            }

        }
        return imagebytes;
    }

    #region ihttphandler members

    public bool isreusable
    {
        get { return false; }
    }

    public void processrequest(httpcontext context)
    {
        context.response.clear();
        context.response.contenttype = getcontenttype(context.request.physicalpath);
        byte[] imagebytes = watermarkimage(context);
        if (imagebytes != null)
        {
            context.response.outputstream.write(imagebytes, 0, imagebytes.length);
        }
        else
        {
            // no bytes = no image which equals no file.   
            // therefore send a 404 - not found response.
            context.response.statuscode = 404;
        }
        context.response.end();
    }

    #endregion
}

第二步,在web.config里添加如下代码:

复制代码 代码如下:

    <httphandlers>
      <!--<add verb="get" type="imagehandler" path="*.jpg,*.png,*.gif,*.bmp"/>-->
      <add verb="get" type="imagehandler" path="uploads/*/*.jpg"/>     
    </httphandlers>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网