C#圖片添加水印的實(shí)現(xiàn)代碼
本文實(shí)例介紹了C#圖片添加水印的實(shí)現(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#實(shí)現(xiàn)圖片添加水印的關(guān)鍵性代碼,希望對大家學(xué)習(xí)C#程序設(shè)計(jì)有所幫助。
上一篇:實(shí)例解析C#設(shè)計(jì)模式編程中簡單工廠模式的使用
欄 目:C#教程
本文標(biāo)題:C#圖片添加水印的實(shí)現(xiàn)代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6688.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)打開畫圖的同時載入圖片、最大化顯示畫圖窗體的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中DataGridView動態(tài)添加行及添加列的方法
- 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10C#操作數(shù)據(jù)庫中存取圖片文件的方法
- 01-10C#圖片處理3種高級應(yīng)用
- 01-10c#實(shí)現(xiàn)識別圖片上的驗(yàn)證碼數(shù)字
- 01-10Silverlight將圖片轉(zhuǎn)換為byte的實(shí)現(xiàn)代碼


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什