在Asp.net中為圖像加入水印信息并保存為Jpg類型
更新時(shí)間:2014年08月22日 11:29:49 投稿:whsnow
這篇文章主要介紹了在Asp.net中為圖像加入水印信息,可定義字體、筆刷等等并保存為Jpg類型,需要的朋友可以參考下
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; //文本的長度
//下面定義一個(gè)矩形區(qū)域,以后在這個(gè)矩形里畫上白底黑字
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();
}
調(diào)用時(shí)很簡單,
AddTextToImg("me.jpg","程序人生http://www.manong123.com/");
一切OK了,感覺.net確實(shí)好強(qiáng)大,這些功能在Asp中可是奢侈品了,而在.Net環(huán)境中卻能輕而易舉的完成!
相關(guān)文章
aspnet_regsql.exe 工具注冊數(shù)據(jù)庫的圖文方法
自 ASP.NET 2.0 起,微軟在 ASP.NET 上新增了很多功能,其中包括 Membership , Role , Profile 等等諸多功能2010-03-03
.net?6?配置QuartZ定時(shí)任務(wù)的過程
這篇文章主要介紹了.net?6?配置QuartZ定時(shí)任務(wù)的過程,在VS2022中,通過Nuget包管理器安裝Quartz?3.8.1 ,這是.net 6 依賴的最高版本,在此記錄學(xué)習(xí)一下,需要的朋友可以參考下2024-04-04
.net 中的SqlConnection連接池機(jī)制詳解
.net 中通過 SqlConnection 連接 sql server,我們會發(fā)現(xiàn)第一次連接時(shí)總是很耗時(shí),但后面連接就很快,這個(gè)其實(shí)和SqlConnection 的連接池機(jī)制有關(guān)2013-04-04
asp.net DropDownList 三級聯(lián)動下拉菜單實(shí)現(xiàn)代碼
asp.net DropDownList 三級聯(lián)動下拉菜單效果代碼,需要的朋友可以參考下。2009-12-12
詳解如何在.NET代碼中使用本地部署的Deepseek語言模型
這篇文章主要來和大家一起聊一聊怎么在?.NET?代碼中使用本地部署的?Deepseek?語言模型,文中的示例代碼簡潔易懂,有需要的小伙伴可以了解下2025-02-02
ASP.NET中RadioButtonList綁定后臺數(shù)據(jù)后觸發(fā)點(diǎn)擊事件
這篇文章主要介紹了ASP.NET中RadioButtonList綁定后臺數(shù)據(jù)后觸發(fā)點(diǎn)擊事件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05

