asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
第一步,添加一個(gè)一般處理程序(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 = "世復(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里添加如下代碼:
<httpHandlers>
<!--<add verb="GET" type="ImageHandler" path="*.jpg,*.png,*.gif,*.bmp"/>-->
<add verb="GET" type="ImageHandler" path="Uploads/*/*.jpg"/>
</httpHandlers>
相關(guān)文章
ASP.NET?MVC實(shí)現(xiàn)多選下拉框保存并顯示
這篇文章介紹了ASP.NET?MVC實(shí)現(xiàn)多選下拉框保存并顯示的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08國(guó)產(chǎn)化之銀河麒麟安裝.NetCore包管理器方式(步驟詳解)
這篇文章主要介紹了國(guó)產(chǎn)化之銀河麒麟安裝.NetCore-包管理器方式,本文給大家分享安裝步驟及安裝命令,對(duì)銀河麒麟安裝.NetCore相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03ASP.NET:設(shè)置頁(yè)面buffer引出來(lái)的問(wèn)題
ASP.NET:設(shè)置頁(yè)面buffer引出來(lái)的問(wèn)題...2006-09-09ASP.Net MVC_DotNetZip簡(jiǎn)單使用方法,解決文件壓縮的問(wèn)題
下面小編就為大家?guī)?lái)一篇ASP.Net MVC_DotNetZip簡(jiǎn)單使用方法,解決文件壓縮的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06asp.net 枚舉文件里面的數(shù)字綁定到DropDownList里面去
將枚舉文件里面的數(shù)字綁定到DropDownList里面去的實(shí)現(xiàn)方法,大家可以借鑒下。2009-07-07asp.net截屏功能實(shí)現(xiàn)截取web頁(yè)面
這篇文章主要介紹了asp.net截屏功能實(shí)現(xiàn)截取web頁(yè)面,是非常實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-08-08