溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用Asp.net怎么為圖像添加水印

發(fā)布時間:2020-12-09 15:38:05 來源:億速云 閱讀:119 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)利用Asp.net怎么為圖像添加水印,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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; //文本的長度
//下面定義一個矩形區(qū)域,以后在這個矩形里畫上白底黑字
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)用時很簡單,

AddTextToImg("me.jpg","程序人生http://www.manong123.com/");

上述就是小編為大家分享的利用Asp.net怎么為圖像添加水印了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI