主页 > 知识库 > asp.net如何在图片上加水印文字具体实现

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

热门标签:人工智能 百度AI接口 电商新玩法 客户服务 国美全国运营中心 科大讯飞语音识别系统 网站排名优化 电销业务

第一步,添加一个一般处理程序(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>

您可能感兴趣的文章:
  • 如何在ASP.NET Core中给上传图片功能添加水印实例代码
  • ASP.NET百度Ueditor编辑器实现上传图片添加水印效果
  • Asp.net开发之webform图片水印和图片验证码的实现方法
  • asp.net继承IHttpHandler接口实现给网站图片添加水印功能实例
  • ASP.NET简单好用功能齐全图片上传工具类(水印、缩略图、裁剪等)
  • Asp.net简单实现给图片增加文字水印
  • asp.net上传图片并作处理水印与缩略图的实例代码
  • ASP.NET 图片加水印防盗链实现代码
  • asp.net中上传图片文件实现防伪图片水印并写入数据库
  • ASP.NET实现图片自动添加水印

标签:益阳 POS机 拉萨 南平 咸宁 厦门 攀枝花 枣庄

巨人网络通讯声明:本文标题《asp.net如何在图片上加水印文字具体实现》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266