欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net如何在图片上加水印文字具体实现】,下面是详细的讲解!
asp.net如何在图片上加水印文字具体实现
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
{
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));
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
}
关于asp.net如何在图片上加水印文字具体实现的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【asp】asp.net 页面转向 Response.Redirect Ser
- 【jQuery】jQuery实现倒计时跳转的例子-倒计时跳
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
