婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

asp.net如何在圖片上加水印文字具體實現

熱門標簽:怎樣給陜西地圖標注顏色 廣州銷售外呼系統定制 400電話辦理信任翰諾科技 福州人工智能電銷機器人加盟 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實現圖片自動添加水印

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

巨人網絡通訊聲明:本文標題《asp.net如何在圖片上加水印文字具體實現》,本文關鍵詞  asp.net,如,何在,圖片,上加,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net如何在圖片上加水印文字具體實現》相關的同類信息!
  • 本頁收集關于asp.net如何在圖片上加水印文字具體實現的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 定结县| 当阳市| 西青区| 宾川县| 兰溪市| 衡阳市| 平武县| 沙田区| 敖汉旗| 册亨县| 扎囊县| 监利县| 高邑县| 共和县| 西充县| 湖口县| 左贡县| 塔河县| 原阳县| 剑川县| 林周县| 丘北县| 新营市| 阿拉善左旗| 灌云县| 乐昌市| 临泽县| 苏尼特左旗| 高唐县| 天门市| 南宫市| 明溪县| 安乡县| 上杭县| 白朗县| 锡林浩特市| 南乐县| 威海市| 察隅县| 丰顺县| 南漳县|