asp.net 添加水印的代码(已测试)
程序员文章站
2024-03-07 22:28:51
加水印的功能代码如下所示 复制代码 代码如下: /// /// 图片修改类,主要是用来保护图片版权的,版权归原作者所有 ///
加水印的功能代码如下所示
/// <summary>
/// 图片修改类,主要是用来保护图片版权的,版权归原作者所有
/// </summary>
public class picmark
{
#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 picmark()
{
}
#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
}
前台代码如下所示
<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="demo.webform1" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="fileupload1" runat="server" />
<br />
<asp:button id="button1" runat="server" onclick="button1_click" text="button" />
</div>
</form>
</body>
</html>
cs类的代码如下所示
protected void button1_click(object sender, eventargs e)
{
string extension = path.getextension(this.fileupload1.filename).toupper();
string filename = guid.newguid().tostring();
string savepath = server.mappath("../upfile/" + filename+ extension);
if (!directory.exists(path.getdirectoryname(savepath)))
{
directory.createdirectory(path.getdirectoryname(savepath));
}
this.fileupload1.saveas(savepath);
//实例化类
picmark wm = new picmark();
wm.drawedimagepath = server.mappath("/upfile/" + "backlogo.gif") ;
wm.modifyimagepath = savepath;
wm.rightspace = 145;
wm.bottoamspace =17;
wm.lucencypercent = 50;
wm.outpath = server.mappath("/upfile/" + filename.replace("-","").toupper() + extension);
wm.drawimage();
//filename = "_new_" + filename;
//string spath = server.mappath("../upfile/" + filename + extension);
//this.fileupload1.saveas(spath);
//保存加水印过后的图片,删除原始图片
if (file.exists(savepath))
{
file.delete(savepath);
//file.delete(wm.outpath);
}
复制代码 代码如下:
/// <summary>
/// 图片修改类,主要是用来保护图片版权的,版权归原作者所有
/// </summary>
public class picmark
{
#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 picmark()
{
}
#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
}
前台代码如下所示
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="demo.webform1" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="fileupload1" runat="server" />
<br />
<asp:button id="button1" runat="server" onclick="button1_click" text="button" />
</div>
</form>
</body>
</html>
cs类的代码如下所示
复制代码 代码如下:
protected void button1_click(object sender, eventargs e)
{
string extension = path.getextension(this.fileupload1.filename).toupper();
string filename = guid.newguid().tostring();
string savepath = server.mappath("../upfile/" + filename+ extension);
if (!directory.exists(path.getdirectoryname(savepath)))
{
directory.createdirectory(path.getdirectoryname(savepath));
}
this.fileupload1.saveas(savepath);
//实例化类
picmark wm = new picmark();
wm.drawedimagepath = server.mappath("/upfile/" + "backlogo.gif") ;
wm.modifyimagepath = savepath;
wm.rightspace = 145;
wm.bottoamspace =17;
wm.lucencypercent = 50;
wm.outpath = server.mappath("/upfile/" + filename.replace("-","").toupper() + extension);
wm.drawimage();
//filename = "_new_" + filename;
//string spath = server.mappath("../upfile/" + filename + extension);
//this.fileupload1.saveas(spath);
//保存加水印过后的图片,删除原始图片
if (file.exists(savepath))
{
file.delete(savepath);
//file.delete(wm.outpath);
}