c#图片添加水印的实例代码
程序员文章站
2023-12-20 12:16:10
复制代码 代码如下:using system;using system.drawing;using system.drawing.drawing2d;using syste...
复制代码 代码如下:
using system;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.io;
namespace imagedrawing
{
/// <summary>
/// 图片修改类,主要是用来保护图片版权的
/// </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>
/// 获取或设置要修改的图像路径
/// </summary>
public string modifyimagepath
{
get{return this.modifyimagepath;}
set{this.modifyimagepath=value;}
}
/// <summary>
/// 获取或设置在画的图片路径(水印图片)
/// </summary>
public string drawedimagepath
{
get{return this.drawedimagepath;}
set{this.drawedimagepath=value;}
}
/// <summary>
/// 获取或设置水印在修改图片中的右边距
/// </summary>
public int rightspace
{
get{return this.rightspace;}
set{this.rightspace=value;}
}
//获取或设置水印在修改图片中距底部的高度
public int bottoamspace
{
get{return this.bottoamspace;}
set{this.bottoamspace=value;}
}
/// <summary>
/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
/// </summary>
public int lucencypercent
{
get{return this.lucencypercent;}
set
{
if(value>=0&&value<=100)
this.lucencypercent=value;
}
}
/// <summary>
/// 获取或设置要输出图像的路径
/// </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
{
//建立图形对象
modifyimage=image.fromfile(this.modifyimagepath);
drawedimage=image.fromfile(this.drawedimagepath);
g=graphics.fromimage(modifyimage);
//获取要绘制图形坐标
int x=modifyimage.width-this.rightspace;
int y=modifyimage.height-this.bottoamspace;
//设置颜色矩阵
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
}
}