利用Asp.net怎么为图像添加水印-创新互联

这期内容当中小编将会给大家带来有关利用Asp.net怎么为图像添加水印,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联公司专业提供成都网站制作、网站设计,网页设计,网站制作(企业站、成都响应式网站建设公司、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

private void AddTextToImg(string fileName,string text)
{
if(!File.Exists(MapPath(fileName)))
{
throw new FileNotFoundException("The file don't exist!");
}

if( text == string.Empty )
{
return;
}
//还需要判断文件类型是否为图像类型,这里就不赘述了

System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);

float fontSize = 12.0f; //字体大小
float textWidth = text.Length*fontSize; //文本的长度
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectX = 0; 
float rectY = 0;
float rectWidth = text.Length*(fontSize+8);
float rectHeight = fontSize+8;
//声明矩形域
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);

Font font = new Font("宋体",fontSize); //定义字体
Brush whiteBrush = new SolidBrush(Color.White); //白笔刷,画文字用
Brush blackBrush = new SolidBrush(Color.Black); //黑笔刷,画背景用

g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight); 

g.DrawString(text,font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream( );
//保存为Jpg类型
bitmap.Save(ms,ImageFormat.Jpeg);

//输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite( ms.ToArray() );

g.Dispose();
bitmap.Dispose();
image.Dispose();
}

新闻名称:利用Asp.net怎么为图像添加水印-创新互联
URL网址:http://ybzwz.com/article/iiihs.html