c#圖片添加水印的實(shí)例代碼
更新時(shí)間:2013年07月23日 10:10:11 作者:
這篇文章介紹了c#圖片添加水印的實(shí)例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageDrawing
{
/// <summary>
/// 圖片修改類,主要是用來保護(hù)圖片版權(quán)的
/// </summary>
public class ImageModification
{
#region "member fields"
private string modifyImagePath=null;
private string drawedImagePath=null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent=70;
private string outPath=null;
#endregion
public ImageModification()
{
}
#region "propertys"
/// <summary>
/// 獲取或設(shè)置要修改的圖像路徑
/// </summary>
public string ModifyImagePath
{
get{return this.modifyImagePath;}
set{this.modifyImagePath=value;}
}
/// <summary>
/// 獲取或設(shè)置在畫的圖片路徑(水印圖片)
/// </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;}
}
//獲取或設(shè)置水印在修改圖片中距底部的高度
public int BottoamSpace
{
get{return this.bottoamSpace;}
set{this.bottoamSpace=value;}
}
/// <summary>
/// 獲取或設(shè)置要繪制水印的透明度,注意是原來圖片透明度的百分比
/// </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 "methods"
/// <summary>
/// 開始繪制水印
/// </summary>
public void DrawImage()
{
Image modifyImage=null;
Image drawedImage=null;
Graphics g=null;
try
{
//建立圖形對(duì)象
modifyImage=Image.FromFile(this.ModifyImagePath);
drawedImage=Image.FromFile(this.DrawedImagePath);
g=Graphics.FromImage(modifyImage);
//獲取要繪制圖形坐標(biāo)
int x=modifyImage.Width-this.rightSpace;
int y=modifyImage.Height-this.BottoamSpace;
//設(shè)置顏色矩陣
float[][] matrixItems ={
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
}
}
您可能感興趣的文章:
- 深入分析WPF客戶端讀取高清圖片卡以及縮略圖的解決方法詳解
- WPF TextBox和PasswordBox添加水印
- WPF 自定義雷達(dá)圖開發(fā)實(shí)例教程
- 在WinForm和WPF中使用GMap.Net地圖插件簡單教程
- C#(.net)水印圖片的生成完整實(shí)例
- C# 添加圖片水印類實(shí)現(xiàn)代碼
- .net c# gif動(dòng)畫如何添加圖片水印實(shí)現(xiàn)思路及代碼
- C#給圖片添加水印完整實(shí)例
- C#給圖片加水印的簡單實(shí)現(xiàn)方法
- C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法
- WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】
相關(guān)文章
C# 創(chuàng)建控制臺(tái)應(yīng)用程序
這篇文章主要介紹了C# 創(chuàng)建控制臺(tái)應(yīng)用程序,在學(xué)習(xí)C#語言的時(shí)候,首先要學(xué)習(xí)控制臺(tái)的應(yīng)用程序,這樣才能專注于語言的學(xué)習(xí),減少學(xué)習(xí)的梯度,也有利于輸出自己需要輸出的內(nèi)容,一定要先使用控制臺(tái)的應(yīng)用程序的方式,下面就和小編一起學(xué)習(xí)該內(nèi)容吧2021-10-10
Unity延時(shí)執(zhí)行的多種方法小結(jié)
本文主要介紹了4種延時(shí)執(zhí)行的方法,主要包括Update計(jì)時(shí)器,Invoke,協(xié)程,DoTween,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法,在項(xiàng)目開發(fā)中有應(yīng)用價(jià)值,需要的朋友可以參考下2014-08-08
WPF實(shí)現(xiàn)自定義Panel面板的示例詳解
WPF中的Panel(面板),是繼承自FrameworkElement的抽象類,表示一個(gè)可以用來排列子元素的面板,本文主要來和大家聊聊WPF如何實(shí)現(xiàn)自定義Panel,感興趣的可以了解下2023-09-09
C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例
本文主要介紹了C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C#中使用Spire.XLS來操作Excel數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了C#中使用Spire.XLS來操作Excel數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

