C#圖片添加水印的實(shí)現(xiàn)代碼
本文實(shí)例介紹了C#圖片添加水印的實(shí)現(xiàn)方法,可以為圖片加文字水印,及判斷是否是圖片文件,分享給大家供大家參考,具體內(nèi)容如下
效果圖:
以下是HovercWarter類(lèi)的代碼:
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; //文本的長(zhǎng)度
//下面定義一個(gè)矩形區(qū)域,以后在這個(gè)矩形里畫(huà)上白底黑字
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); //白筆刷,畫(huà)文字用
Brush blackBrush = new SolidBrush(Color.Black); //黑筆刷,畫(huà)背景用
g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
g.DrawString(text, font, whiteBrush, textArea);
MemoryStream ms = new MemoryStream();
//保存為Jpg類(lèi)型
bitmap.Save(ms, ImageFormat.Jpeg);
Image h_hovercImg = Image.FromStream(ms);
g.Dispose();
bitmap.Dispose();
return h_hovercImg;
}
/// <summary>
/// 根據(jù)文件頭判斷上傳的文件類(lèi)型
/// </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")
//何問(wèn)起 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)鍵性代碼,希望對(duì)大家學(xué)習(xí)C#程序設(shè)計(jì)有所幫助。
上一篇:實(shí)例解析C#設(shè)計(jì)模式編程中簡(jiǎn)單工廠模式的使用
欄 目:C#教程
本文標(biāo)題:C#圖片添加水印的實(shí)現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6688.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10使用Nopcommerce為商城添加滿(mǎn)XX減XX優(yōu)惠券功能
- 01-10C#中DataGridView動(dòng)態(tài)添加行及添加列的方法
- 01-10http圖片上傳安全性問(wèn)題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
- 01-10C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法
- 01-10C#圖片處理3種高級(jí)應(yīng)用
- 01-10c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字
- 01-10Silverlight將圖片轉(zhuǎn)換為byte的實(shí)現(xiàn)代碼


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線(xiàn)程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


