濮阳杆衣贸易有限公司

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

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

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

第一步,添加一個一般處理程序(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編輯器實現(xiàn)上傳圖片添加水印效果
  • Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現(xiàn)方法
  • asp.net繼承IHttpHandler接口實現(xiàn)給網(wǎng)站圖片添加水印功能實例
  • ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
  • Asp.net簡單實現(xiàn)給圖片增加文字水印
  • asp.net上傳圖片并作處理水印與縮略圖的實例代碼
  • ASP.NET 圖片加水印防盜鏈實現(xiàn)代碼
  • asp.net中上傳圖片文件實現(xiàn)防偽圖片水印并寫入數(shù)據(jù)庫
  • ASP.NET實現(xiàn)圖片自動添加水印

標簽:宜春 綿陽 延安 大興安嶺 黃南 曲靖 焦作 新疆

巨人網(wǎng)絡通訊聲明:本文標題《asp.net如何在圖片上加水印文字具體實現(xiàn)》,本文關鍵詞  asp.net,如,何在,圖片,上加,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net如何在圖片上加水印文字具體實現(xiàn)》相關的同類信息!
  • 本頁收集關于asp.net如何在圖片上加水印文字具體實現(xiàn)的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    遂昌县| 郧西县| 南通市| 陇南市| 思南县| 军事| 灵石县| 广宁县| 南通市| 怀柔区| 象州县| 垦利县| 万全县| 广东省| 施甸县| 平潭县| 富锦市| 绥江县| 娄烦县| 渑池县| 威信县| 尉犁县| 易门县| 观塘区| 霍山县| 方正县| 黑山县| 报价| 宜兰县| 宁南县| 呈贡县| 南木林县| 砚山县| 上饶县| 襄汾县| 江孜县| 赤峰市| 余干县| 新化县| 红河县| 松桃|