温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

asp.net在图片上加水印文字的实现方法

发布时间:2021-02-04 11:05:28 来源:亿速云 阅读:177 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关asp.net在图片上加水印文字的实现方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

这篇文章将为大家详细讲解有关asp.net在图片上加水印文字的实现方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

第一步,添加一个一般处理程序(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在图片上加水印文字的实现方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI