asp.net如何在图片上加水印文字具体实现
第一步,添加一个一般处理程序(Handler),本例是ImageHandler
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Net.Mime;
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.Drawing.Drawing2D;
///<summary>
///SummarydescriptionforImageHandler
///</summary>
publicclassImageHandler:IHttpHandler
{
publicImageHandler()
{
}
publicstringGetContentType(Stringpath)
{
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;
}
returnString.Empty;
}
publicImageFormatGetImageFormat(Stringpath)
{
switch(Path.GetExtension(path).ToLower())
{
case".bmp":returnImageFormat.Bmp;
case".gif":returnImageFormat.Gif;
case".jpg":returnImageFormat.Jpeg;
case".png":returnImageFormat.Png;
default:returnnull;
}
}
protectedbyte[]WatermarkImage(HttpContextcontext)
{
byte[]imageBytes=null;
if(File.Exists(context.Request.PhysicalPath))
{
//Normallyyou"dputthisinaconfigfilesomewhere.
stringwatermark="世复检测";
Imageimage=Image.FromFile(context.Request.PhysicalPath);
Graphicsgraphic;
if(image.PixelFormat!=PixelFormat.Indexed&&image.PixelFormat!=PixelFormat.Format8bppIndexed&&image.PixelFormat!=PixelFormat.Format4bppIndexed&&image.PixelFormat!=PixelFormat.Format1bppIndexed)
{
//GraphicisnotaIndexed(GIF)image
graphic=Graphics.FromImage(image);
}
else
{
/*Cannotcreateagraphicsobjectfromanindexed(GIF)image.
*Sowe"regoingtocopytheimageintoanewbitmapso
*wecanworkwithit.*/
BitmapindexedImage=newBitmap(image);
graphic=Graphics.FromImage(indexedImage);
//Drawthecontentsoftheoriginalbitmapontothenewbitmap.
graphic.DrawImage(image,0,0,image.Width,image.Height);
image=indexedImage;
}
graphic.SmoothingMode=SmoothingMode.AntiAlias&SmoothingMode.HighQuality;
FontmyFont=newFont("Arial",15);
SolidBrushbrush=newSolidBrush(Color.FromArgb(255,Color.Red));
/*Thisgetsthesizeofthegraphicsowecandetermine
*theloopcountsandplacementofthewatermarkedtext.*/
SizeFtextSize=graphic.MeasureString(watermark,myFont);
////Writethetextacrosstheimage.
//for(inty=0;y<image.Height;y++)
//{
// for(intx=0;x<image.Width;x++)
// {
// PointFpointF=newPointF(x,y);
// graphic.DrawString(watermark,myFont,brush,pointF);
// x+=Convert.ToInt32(textSize.Width);
// }
// y+=Convert.ToInt32(textSize.Height);
//}
//Writethetextattherightbottomoftheimage.
for(inty=image.Height-25;y<image.Height;y++)
{
for(intx=image.Width-100;x<image.Width;x++)
{
PointFpointF=newPointF(x,y);
graphic.DrawString(watermark,myFont,brush,pointF);
x+=Convert.ToInt32(textSize.Width);
}
y+=Convert.ToInt32(textSize.Height);
}
using(MemoryStreammemoryStream=newMemoryStream())
{
image.Save(memoryStream,GetImageFormat(context.Request.PhysicalPath));
imageBytes=memoryStream.ToArray();
}
}
returnimageBytes;
}
#regionIHttpHandlerMembers
publicboolIsReusable
{
get{returnfalse;}
}
publicvoidProcessRequest(HttpContextcontext)
{
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
{
//Nobytes=noimagewhichequalsNOFILE.
//Thereforesenda404-notfoundresponse.
context.Response.StatusCode=404;
}
context.Response.End();
}
#endregion
}
第二步,在web.config里添加如下代码:
<httpHandlers>
<!--<addverb="GET"type="ImageHandler"path="*.jpg,*.png,*.gif,*.bmp"/>-->
<addverb="GET"type="ImageHandler"path="Uploads/*/*.jpg"/>
</httpHandlers>
相关文章
- ASP.NET MVC (五、HttpClient接口解析)
- ASP结合MySQL:实现快速数据库连接(asp如何连接mysql)
- 数据库使用.NET连接MySQL数据库(net连接mysql)
- ASP.NET 大文件下载的实现思路及代码
- ASP连接MySQL实现优雅的数据查询(asp连接查询mysql)
- ASP绑定MySQL实现功能强大的数据库访问(asp绑定mysql)
- ASP与MySQL的联合开发实现轻松跨平台互联网应用(asp和mysql)
- ASP操作MySQL简易指南轻松删除记录(asp删除记录mysql)
- NET与MySQL实现无缝连接(.net mysql连接)
- Redis集群实现高性能NET应用(redis集群 .net)
- asp.net用url重写URLReWriter实现任意二级域名
- ASP.NET路径问题的解决方法
- asp.net下xml当作导航数据源实现动态权限
- 用js脚本控制asp.net下treeview的NodeCheck的实现代码
- CentOS5.1下跑Mono和Asp.net的实现方法
- asp.net正则表达式删除指定的HTML标签的代码
- asp.net下按钮点击后禁用的实现代码
- ASP.NET性能优化之局部缓存分析
- ASP.NET中实现jQueryValidation-Engine的Ajax验证实现代码
- ASP.Net中英文复合检索文本框实现思路及代码
- Asp.Net无刷新文件上传并显示进度条的实现方法及思路
- 在ASP.NET中实现弹出日历的具体方法
- ASP.NET防止按钮多次提交核心实现代码
- asp.net获取select值的方法
- ASP.NET中MVC从后台控制器传递数据到前台视图的方式
- asp.net中C#实现手动回收内存的方法