zl程序教程

您现在的位置是:首页 >  后端

当前栏目

asp.net添加水印的代码(已测试)

NetASP测试代码 添加 水印
2023-06-13 09:14:17 时间
加水印的功能代码如下所示
复制代码代码如下:

///<summary>
///图片修改类,主要是用来保护图片版权的,版权归原作者所有
///</summary>
publicclasspicmark
{
#region"memberfields"
privatestringmodifyImagePath=null;
privatestringdrawedImagePath=null;
privateintrightSpace;
privateintbottoamSpace;
privateintlucencyPercent=70;
privatestringoutPath=null;
#endregion
publicpicmark()
{
}
#region"propertys"
///<summary>
///获取或设置要修改的图像路径
///</summary>
publicstringModifyImagePath
{
get{returnthis.modifyImagePath;}
set{this.modifyImagePath=value;}
}
///<summary>
///获取或设置在画的图片路径(水印图片)
///</summary>
publicstringDrawedImagePath
{
get{returnthis.drawedImagePath;}
set{this.drawedImagePath=value;}
}
///<summary>
///获取或设置水印在修改图片中的右边距
///</summary>
publicintRightSpace
{
get{returnthis.rightSpace;}
set{this.rightSpace=value;}
}
//获取或设置水印在修改图片中距底部的高度
publicintBottoamSpace
{
get{returnthis.bottoamSpace;}
set{this.bottoamSpace=value;}
}
///<summary>
///获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
///</summary>
publicintLucencyPercent
{
get{returnthis.lucencyPercent;}
set
{
if(value>=0&&value<=100)
this.lucencyPercent=value;
}
}
///<summary>
///获取或设置要输出图像的路径
///</summary>
publicstringOutPath
{
get{returnthis.outPath;}
set{this.outPath=value;}
}
#endregion
#region"methods"
///<summary>
///开始绘制水印
///</summary>
publicvoidDrawImage()
{
ImagemodifyImage=null;
ImagedrawedImage=null;
Graphicsg=null;
try
{
//建立图形对象
modifyImage=Image.FromFile(this.ModifyImagePath);
drawedImage=Image.FromFile(this.DrawedImagePath);
g=Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
intx=modifyImage.Width-this.rightSpace;
inty=modifyImage.Height-this.BottoamSpace;
//设置颜色矩阵
float[][]matrixItems={
newfloat[]{1,0,0,0,0},
newfloat[]{0,1,0,0,0},
newfloat[]{0,0,1,0,0},
newfloat[]{0,0,0,(float)this.LucencyPercent/100f,0},
newfloat[]{0,0,0,0,1}};
ColorMatrixcolorMatrix=newColorMatrix(matrixItems);
ImageAttributesimgAttr=newImageAttributes();
imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(
drawedImage,
newRectangle(x,y,drawedImage.Width,drawedImage.Height),
0,0,drawedImage.Width,drawedImage.Height,
GraphicsUnit.Pixel,imgAttr);
//保存文件
string[]allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
FileInfofile=newFileInfo(this.ModifyImagePath);
ImageFormatimageType=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;
}
MemoryStreamms=newMemoryStream();
modifyImage.Save(ms,imageType);
byte[]imgData=ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStreamfs=null;
if(this.OutPath==null||this.OutPath=="")
{
File.Delete(this.ModifyImagePath);
fs=newFileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
}
else
{
fs=newFileStream(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
}

前台代码如下所示
复制代码代码如下:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="demo.WebForm1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:FileUploadID="FileUpload1"runat="server"/>
<br/>
<asp:ButtonID="Button1"runat="server"onclick="Button1_Click"Text="Button"/>
</div>
</form>
</body>
</html>

cs类的代码如下所示
复制代码代码如下:
protectedvoidButton1_Click(objectsender,EventArgse)
{
stringextension=Path.GetExtension(this.FileUpload1.FileName).ToUpper();
stringfileName=Guid.NewGuid().ToString();
stringsavePath=Server.MapPath("../upfile/"+fileName+extension);
if(!Directory.Exists(Path.GetDirectoryName(savePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
}
this.FileUpload1.SaveAs(savePath);
//实例化类
picmarkwm=newpicmark();
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;
//stringsPath=Server.MapPath("../upfile/"+fileName+extension);
//this.FileUpload1.SaveAs(sPath);
//保存加水印过后的图片,删除原始图片
if(File.Exists(savePath))
{
File.Delete(savePath);
//File.Delete(wm.OutPath);
}