主頁(yè) > 知識(shí)庫(kù) > asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)

asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)

熱門標(biāo)簽:怎樣給陜西地圖標(biāo)注顏色 廣州銷售外呼系統(tǒng)定制 400電話辦理信任翰諾科技 福州人工智能電銷機(jī)器人加盟 ai電銷機(jī)器人對(duì)貸款有幫助嗎 電銷機(jī)器人 數(shù)據(jù) 地圖標(biāo)注多少錢一張 宿遷智能外呼系統(tǒng)排名 云狐人工智能電話機(jī)器人

第一步,添加一個(gè)一般處理程序(Handler),本例是ImageHandler

復(fù)制代碼 代碼如下:

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 = "世復(fù)檢測(cè)";

            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里添加如下代碼:

復(fù)制代碼 代碼如下:

    httpHandlers>
      !--add verb="GET" type="ImageHandler" path="*.jpg,*.png,*.gif,*.bmp"/>-->
      add verb="GET" type="ImageHandler" path="Uploads/*/*.jpg"/>     
    /httpHandlers>

您可能感興趣的文章:
  • 如何在ASP.NET Core中給上傳圖片功能添加水印實(shí)例代碼
  • ASP.NET百度Ueditor編輯器實(shí)現(xiàn)上傳圖片添加水印效果
  • Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
  • asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能實(shí)例
  • ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
  • Asp.net簡(jiǎn)單實(shí)現(xiàn)給圖片增加文字水印
  • asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
  • ASP.NET 圖片加水印防盜鏈實(shí)現(xiàn)代碼
  • asp.net中上傳圖片文件實(shí)現(xiàn)防偽圖片水印并寫入數(shù)據(jù)庫(kù)
  • ASP.NET實(shí)現(xiàn)圖片自動(dòng)添加水印

標(biāo)簽:宜春 綿陽(yáng) 延安 大興安嶺 黃南 曲靖 焦作 新疆

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)》,本文關(guān)鍵詞  asp.net,如,何在,圖片,上加,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章