C#.NET 圖片水印添加代碼
本文實(shí)例為大家分享了C#.NET添加 圖片水印的方法,供大家參考,具體內(nèi)容如下
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; namespace Pub.Class { /// <summary> /// 添加水印類(lèi) 只支持添加圖片水印 /// </summary> public class Watermark { #region 私有成員 private string modifyImagePath=null; private string drawedImagePath=null; private int rightSpace; private int bottoamSpace; private int lucencyPercent=70; private string outPath=null; #endregion #region 構(gòu)造器 /// <summary> /// 構(gòu)造函數(shù) /// </summary> public Watermark() { } #endregion #region 屬性 /// <summary> /// 獲取或設(shè)置要修改的圖像路徑 /// </summary> public string ModifyImagePath { get{return this.modifyImagePath;} set{this.modifyImagePath=value;} } /// <summary> /// 獲取或設(shè)置在畫(huà)的圖片路徑(水印圖片) /// </summary> public string DrawedImagePath { get{return this.drawedImagePath;} set{this.drawedImagePath=value;} } /// <summary> /// 獲取或設(shè)置水印在修改圖片中的右邊距 /// </summary> public int RightSpace { get{return this.rightSpace;} set{this.rightSpace=value;} } /// <summary> /// 獲取或設(shè)置水印在修改圖片中距底部的高度 /// </summary> public int BottoamSpace { get{return this.bottoamSpace;} set{this.bottoamSpace=value;} } /// <summary> /// 獲取或設(shè)置要繪制水印的透明度,注意是原來(lái)圖片透明度的百分比 /// </summary> public int LucencyPercent { get{return this.lucencyPercent;} set { if(value>=0&&value<=100) this.lucencyPercent=value; } } /// <summary> /// 獲取或設(shè)置要輸出圖像的路徑 /// </summary> public string OutPath { get{return this.outPath;} set{this.outPath=value;} } #endregion #region 開(kāi)始繪制水印 DrawImage /// <summary> /// 開(kāi)始繪制水印 /// </summary> /// <example> /// <code> /// Watermark wm = new Watermark(); /// wm.DrawedImagePath= Server.MapPath("") + "/upfile/" + "backlogo.gif"; /// wm.ModifyImagePath=path; /// wm.RightSpace=184; /// wm.BottoamSpace=81; /// wm.LucencyPercent=50; /// wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension; /// wm.DrawImage(); /// /// //保存加水印過(guò)后的圖片,刪除原始圖片 /// mFileName=fileName + "_new" + extension; /// if(File.Exists(path)) { File.Delete(path); } /// </code> /// </example> public void DrawImage() { Image modifyImage=null; Image drawedImage=null; Graphics g=null; try { modifyImage=Image.FromFile(this.ModifyImagePath);//建立圖形對(duì)象 drawedImage=Image.FromFile(this.DrawedImagePath); g=Graphics.FromImage(modifyImage); int x=modifyImage.Width-this.rightSpace;//獲取要繪制圖形坐標(biāo) int y=modifyImage.Height-this.BottoamSpace; float[][] matrixItems ={//設(shè)置顏色矩陣 new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imgAttr=new ImageAttributes(); imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap); g.DrawImage(//繪制陰影圖像 drawedImage, new Rectangle(x,y,drawedImage.Width,drawedImage.Height), 0,0,drawedImage.Width,drawedImage.Height, GraphicsUnit.Pixel,imgAttr); string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};//保存文件 FileInfo file=new FileInfo(this.ModifyImagePath); ImageFormat imageType=ImageFormat.Gif; switch(file.Extension.ToLower()) { case ".jpg": imageType=ImageFormat.Jpeg; break; case ".gif": imageType=ImageFormat.Gif; break; case ".png": imageType=ImageFormat.Png; break; case ".bmp": imageType=ImageFormat.Bmp; break; case ".tif": imageType=ImageFormat.Tiff; break; case ".wmf": imageType=ImageFormat.Wmf; break; case ".ico": imageType=ImageFormat.Icon; break; default: break; } MemoryStream ms=new MemoryStream(); modifyImage.Save(ms,imageType); byte[] imgData=ms.ToArray(); modifyImage.Dispose(); drawedImage.Dispose(); g.Dispose(); FileStream fs=null; if(this.OutPath==null || this.OutPath=="") { File.Delete(this.ModifyImagePath); fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write); } else { fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write); } if(fs!=null) { fs.Write(imgData,0,imgData.Length); fs.Close(); } } finally { try { drawedImage.Dispose(); modifyImage.Dispose(); g.Dispose(); } catch{} } } #endregion } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#語(yǔ)言基礎(chǔ)——結(jié)構(gòu)體和枚舉類(lèi)型全面解析
本文標(biāo)題:C#.NET 圖片水印添加代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6349.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10http圖片上傳安全性問(wèn)題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
- 01-10C#.NET實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)登錄的方法
- 01-10C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法
- 01-10C#圖片處理3種高級(jí)應(yīng)用
- 01-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
- 01-10C#.Net ArrayList的使用方法
- 01-10c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字


閱讀排行
- 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ī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什