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

加水印的功能代码如下所示


代码如下:

/// <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);
}

(0)

相关推荐

  • ASP.Net 上传图片并生成高清晰缩略图

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT

  • asp.net文件上传功能(单文件,多文件,自定义生成缩略图,水印)

    前言 上传功能,是大家经常用到了,可能每一个项目都可以会用到.网上到处都有上传功能的代码.比我写的好的有很多.我这里也仅是分享我的代码. 功能实现点 1.单个文件上传: 2.多个文件上传: 3.对于图片等类型的图像,可以自定义生成缩略图大小: 4.文件服务器扩展. 模式 主要使用的是"模板方法"的设计模式. 本文章的功能优缺点 1.可以自定义生成缩略图的大小,任意定义.对于像微生活运动户外商城(http://sports.8t8x.com/) .淘宝网等的网站,他们需要上传大量的商品图

  • ASP.NET简单好用功能齐全图片上传工具类(水印、缩略图、裁剪等)

    使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui.SetPicWater = Server.MapPath("2.png");//图片水印(图片和文字都赋值图片有效) ui.SetPositionWater = 4;//水印图片的位置 0居中.1左上角.2右上角.3左下角.4右下角 ui.SetSmallImgHeight = &quo

  • ASP.NET实现上传图片并生成缩略图的方法

    本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法.分享给大家供大家参考,具体如下: protected void bt_upload_Click(object sender, EventArgs e) { //检查上传文件的格式是否有效 if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { Response.Write("上传图片格式无效!"); re

  • asp.net实现生成缩略图及给原始图加水印的方法示例

    本文实例讲述了asp.net实现生成缩略图及给原始图加水印的方法.分享给大家供大家参考,具体如下: using System.IO; using System.Drawing.Imaging; private void Button1_ServerClick(object sender, System.EventArgs e) { Graphics g=null; System.Drawing.Image upimage=null; System.Drawing.Image thumimg=nu

  • asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧

    public class MyGDI {     public static void CreateWatermark(string sSrcFilePath, string sDstFilePath, string sText1, string sColor1, string sSize1, string sFont1, string sText2, string sColor2, string sSize2, string sFont2, string sBgColor, string sT

  • Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)

    复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; usi

  • asp.net上传图片并作处理水印与缩略图的实例代码

    方法类: 复制代码 代码如下: upFileClass.cs using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlC

  • asp.net如何在图片上加水印文字具体实现

    第一步,添加一个一般处理程序(Handler),本例是ImageHandler 复制代码 代码如下: using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;usin

  • asp.net 上传图片并同时生成缩略图的代码

    复制代码 代码如下: <%@ Page Language="C#" ResponseEncoding="gb2312" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Drawing" %> <%@ I

  • asp.net图片上传生成缩略图的注意事项

    bitmap.Save(imgPath,ImageFormat.Jpeg);   //这是保存缩略图的一段代码,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本来就是jpg格式的,也不能去掉.因为如果去掉的话,生成的缩略图比原始图片还要大! //另外,imgPath必须首先创建,否则会产生GDI+的一般性错误. path=System.Web.HttpContext.Current.Server.MapPath(path); 使用if(!System.IO.Director

随机推荐