C#實(shí)現(xiàn)給圖片添加文字水印的示例代碼
應(yīng)用場(chǎng)景
在某些應(yīng)用項(xiàng)目(如電子檔案信息管理)中,查看電子圖片信息是經(jīng)常使用到的功能,此時(shí)我們就需要給顯示在瀏覽器中的圖片添加文字水印版權(quán)或提示信息。增加水印主要起到如下作用:
1、防止盜圖:圖片加水印可以有效防止盜圖,將文字水印嵌入到圖片中作為特殊標(biāo)記,可以在不影響圖片質(zhì)量的情況下保護(hù)版權(quán),即使別人下載了圖片,也可以通過水印追蹤到圖片的來源。
2、增加宣傳效果:可以通過添加URL或其它宣傳性文字,增加宣傳效果。
開發(fā)運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
方法說明
AddWaterText 方法無返回值,具體參數(shù)說明請(qǐng)參照下表:
| 序號(hào) | 參數(shù)名 | 類型 | 說明 |
|---|---|---|---|
| 1 | oldpath | string | 原圖片文件路徑 |
| 2 | text | string | 要添加的水印文字 |
| 3 | newpath | string | 新輸出圖片文件路徑 |
| 4 | point | object | 設(shè)置文字起始位置坐標(biāo) |
| 5 | font | System.Drawing.Font | 設(shè)置文字的字體 |
| 6 | color | System.Drawing.Color | 設(shè)置文字的顏色 可使用 System.Drawing.Color.FromArgb(alpha, r, g,b)方法添加濾鏡效果 |
| 7 | rotate | float | 旋轉(zhuǎn)角度值,默認(rèn)值為 0.0f |
| 8 | textWidth | int | 文本預(yù)估寬度,默認(rèn)值為1 |
| 9 | textHeight | int | 文本預(yù)估高度,默認(rèn)值為1 |
| 10 | repeatD | int | 多水印文本間距值,默認(rèn)值為0 |
方法代碼
public void AddWaterText(string oldpath, string text, string newpath, object point, System.Drawing.Font font, System.Drawing.Color color, float rotate = 0.0f, int textWidth = 1,int textHeight=1, int repeatD=0)
{
try
{
FileStream fs = new FileStream(oldpath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.Clear(Color.FromName("white"));
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel);
System.Drawing.SizeF crSize = new SizeF();
crSize = gbmPhoto.MeasureString(text, font);
float y = imgPhotoHeight - crSize.Height;
float x = imgPhotoWidth - crSize.Width;
System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
StrFormat.Alignment = System.Drawing.StringAlignment.Center;
if(point!=null)
{
System.Drawing.Point newpos=((System.Drawing.Point)point);
x=newpos.X;
y=newpos.Y;
}
System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(color);
System.Drawing.Color.FromArgb(1,1,1,1);
gbmPhoto.RotateTransform(rotate);
if (repeatD == 0)
{
gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
}
else
{
int xcount = imgPhotoWidth/textWidth+3;
int ycount = imgPhotoHeight/textHeight+3;
float ox = x;
for (int k = 0; k < ycount; k++)
{
for (int i = 0; i < xcount; i++)
{
for (int j = 0; j < xcount; j++)
{
gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
}
x += textWidth+repeatD;
}
x = ox;
y += textHeight+repeatD;
}
}
bmPhoto.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
gbmPhoto.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
}
catch
{
;
}
}調(diào)用示例
//獲取源圖片文件路徑
string tempfile=Request.PhysicalApplicationPath+"\\app_data\\test.jpg";
//設(shè)置文字位置
System.Drawing.Point point = new System.Drawing.Point();
point.X = -10;
point.Y = -100;
//設(shè)置字體類
System.Drawing.Font font = new System.Drawing.Font("微軟雅黑", 19, System.Drawing.FontStyle.Bold);
//設(shè)置字體濾鏡值 ,和RGB分量顏色
int alpha = 25; int r = 255; int g = 0; int b = 255;
System.Drawing.Color color = System.Drawing.Color.FromArgb(alpha, r, g, b);
float rotate=30.0f; // 旋轉(zhuǎn)角度
int textWidth = 100; //文本預(yù)估寬度
int textHeight=30; //文本預(yù)估高度
int repeatD=100; // 多水印文本間距,則表示多水印輸出
//添加水印文字
string text="版權(quán)所有";
AddWaterText(tempfile,text,tempfile, point, font, color,rotate,textWidth,textHeight,repeatD);
File.Delete(tempfile); //刪除釋放文件,在些之前可執(zhí)行顯示操作,如獲取base64編碼顯示效果如下圖:

小結(jié)
AddWaterText 方法需要根據(jù)您實(shí)際應(yīng)用中的圖片大小動(dòng)態(tài)調(diào)整參數(shù),以達(dá)到滿意的顯示效果,如果文字起始位置,字體大小,水印間距等。您也可以改造本方法或應(yīng)用,自動(dòng)適應(yīng)調(diào)整參數(shù)值。
調(diào)用示例中新舊圖片文件輸出為同一文件,然后刪除釋放文件所占用磁盤的空間,因此我們想要正確顯示圖片在瀏覽器的話,需要在刪除文件前獲取圖片的Base64編碼即可,如何獲取base64數(shù)據(jù)的方法請(qǐng)參照我的文章:《C# 自動(dòng)填充文字內(nèi)容到指定圖片》
到此這篇關(guān)于C#實(shí)現(xiàn)給圖片添加文字水印的示例代碼的文章就介紹到這了,更多相關(guān)C#圖片添加文字水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C# 泛型中的數(shù)據(jù)類型判定與轉(zhuǎn)換
這篇文章主要介紹了C# 泛型中的數(shù)據(jù)類型判定與轉(zhuǎn)換,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C# winform主界面打開并關(guān)閉登錄界面的方法
這篇文章主要介紹了C# winform主界面打開并關(guān)閉登錄界面的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼
這篇文章主要介紹了C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決
本文主要介紹了Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
這篇文章主要介紹了C#實(shí)現(xiàn)文件壓縮與解壓的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件進(jìn)行zip格式壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
這篇文章主要介紹了c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12

