C#圖片添加水印的實現(xiàn)代碼
更新時間:2022年05月06日 15:33:19 投稿:lijiao
這篇文章主要為大家詳細介紹了C#給圖片添加水印的實現(xiàn)代碼,不僅可以為圖片加文字水印,還可以判斷是否是圖片文件,感興趣的小伙伴們可以參考一下
本文實例介紹了C#圖片添加水印的實現(xiàn)方法,可以為圖片加文字水印,及判斷是否是圖片文件,分享給大家供大家參考,具體內(nèi)容如下
效果圖:
以下是HovercWarter類的代碼:
using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace HoverTreeBatch.HovercFrame { public class HovercWarter { public static Image AddTextToImg(Image image, string text) { 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); Image h_hovercImg = Image.FromStream(ms); g.Dispose(); bitmap.Dispose(); return h_hovercImg; } /// <summary> /// 根據(jù)文件頭判斷上傳的文件類型 /// </summary> /// <param name="filePath">filePath是文件的完整路徑 </param> /// <returns>返回true或false</returns> public static bool IsPicture(string filePath) { try { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fs); string fileClass; byte buffer; buffer = reader.ReadByte(); fileClass = buffer.ToString(); buffer = reader.ReadByte(); fileClass += buffer.ToString(); reader.Close(); fs.Close(); if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677") //何問起 hovertree.com //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar { return true; } else { return false; } } catch { return false; } } } }
以上就是C#實現(xiàn)圖片添加水印的關(guān)鍵性代碼,希望對大家學習C#程序設(shè)計有所幫助。
相關(guān)文章
C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
這篇文章主要為大家詳細介紹了C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04C#12中的Collection expressions集合表達式語法糖詳解
C#12中引入了新的語法糖來創(chuàng)建常見的集合,并且可以使用..來解構(gòu)集合,將其內(nèi)聯(lián)到另一個集合中,下面就跟隨小編一起學習一下C#12中這些語法糖的使用吧2023-11-11C#利用Spire.Pdf包實現(xiàn)為PDF添加數(shù)字簽名
Spire.PDF for .NET 是一款專業(yè)的基于.NET平臺的PDF文檔控制組件。它能夠讓開發(fā)人員在不使用Adobe Acrobat和其他外部控件的情況下,運用.NET 應(yīng)用程序創(chuàng)建,閱讀,編寫和操縱PDF 文檔。本文將利用其實現(xiàn)添加數(shù)字簽名,需要的可以參考一下2022-08-08C#使用NPOI實現(xiàn)Excel導(dǎo)入導(dǎo)出功能
這篇文章主要為大家詳細介紹了C#使用NPOI實現(xiàn)Excel導(dǎo)入導(dǎo)出功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02