C#實現(xiàn)給圖片添加文字水印的示例代碼
應用場景
在某些應用項目(如電子檔案信息管理)中,查看電子圖片信息是經(jīng)常使用到的功能,此時我們就需要給顯示在瀏覽器中的圖片添加文字水印版權或提示信息。增加水印主要起到如下作用:
1、防止盜圖:圖片加水印可以有效防止盜圖,將文字水印嵌入到圖片中作為特殊標記,可以在不影響圖片質(zhì)量的情況下保護版權,即使別人下載了圖片,也可以通過水印追蹤到圖片的來源。
2、增加宣傳效果:可以通過添加URL或其它宣傳性文字,增加宣傳效果。
開發(fā)運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
方法說明
AddWaterText 方法無返回值,具體參數(shù)說明請參照下表:
序號 | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | oldpath | string | 原圖片文件路徑 |
2 | text | string | 要添加的水印文字 |
3 | newpath | string | 新輸出圖片文件路徑 |
4 | point | object | 設置文字起始位置坐標 |
5 | font | System.Drawing.Font | 設置文字的字體 |
6 | color | System.Drawing.Color | 設置文字的顏色 可使用 System.Drawing.Color.FromArgb(alpha, r, g,b)方法添加濾鏡效果 |
7 | rotate | float | 旋轉角度值,默認值為 0.0f |
8 | textWidth | int | 文本預估寬度,默認值為1 |
9 | textHeight | int | 文本預估高度,默認值為1 |
10 | repeatD | int | 多水印文本間距值,默認值為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"; //設置文字位置 System.Drawing.Point point = new System.Drawing.Point(); point.X = -10; point.Y = -100; //設置字體類 System.Drawing.Font font = new System.Drawing.Font("微軟雅黑", 19, System.Drawing.FontStyle.Bold); //設置字體濾鏡值 ,和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; // 旋轉角度 int textWidth = 100; //文本預估寬度 int textHeight=30; //文本預估高度 int repeatD=100; // 多水印文本間距,則表示多水印輸出 //添加水印文字 string text="版權所有"; AddWaterText(tempfile,text,tempfile, point, font, color,rotate,textWidth,textHeight,repeatD); File.Delete(tempfile); //刪除釋放文件,在些之前可執(zhí)行顯示操作,如獲取base64編碼
顯示效果如下圖:
小結
AddWaterText 方法需要根據(jù)您實際應用中的圖片大小動態(tài)調(diào)整參數(shù),以達到滿意的顯示效果,如果文字起始位置,字體大小,水印間距等。您也可以改造本方法或應用,自動適應調(diào)整參數(shù)值。
調(diào)用示例中新舊圖片文件輸出為同一文件,然后刪除釋放文件所占用磁盤的空間,因此我們想要正確顯示圖片在瀏覽器的話,需要在刪除文件前獲取圖片的Base64編碼即可,如何獲取base64數(shù)據(jù)的方法請參照我的文章:《C# 自動填充文字內(nèi)容到指定圖片》
到此這篇關于C#實現(xiàn)給圖片添加文字水印的示例代碼的文章就介紹到這了,更多相關C#圖片添加文字水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#調(diào)用攝像頭實現(xiàn)拍照功能的示例代碼
這篇文章主要介紹了C#調(diào)用攝像頭實現(xiàn)拍照功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決
本文主要介紹了Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05