Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.IO;
/// <summary>
/// 上传图片加水印
/// 作者:
/// 时间:2006-12-8
/// E-mail:jiay_yong@163.com
/// 说明:文字水印的位置有待完善,建议用图片水印
/// </summary>
public partial class UpFile : System.Web.UI.UserControl
{
string _strNewFileFolder = "UpLoad/"; //文件上传的目录
int _NewImgpx = 300; //缩略图尺寸
bool _IsThumb = true; //是否启用缩略图 true:启用 false:不启用
bool _IsWatermark = true; //是否启用水印 true:启用 false:不启用
int _WatemarkPos = 9; //水印位置 1:左上角 2:水平居中垂直顶部 3:右上角 4:垂直居中水平靠左 5:居中 6:垂直居中水平靠右 7:左下角 8:水平居中垂直底部 9:右下角
string _WatemarkType = "img"; //水印类型 txt:文字水印 img:图片水印
string _WatemarkText = "PayOpen薪酬公开分析网"; //水印文字
string _WatemarkFont = "Verdana"; //水印字体
int _WatemarkFontSize = 12; //水印字号
string _WatemarkImg = "mark.png"; //水印图片
int _FileSizeMax = 2048; //限制上传文件大小
string _srcFileName; //上传文件原文件名
string _srcFileExtension; //扩展名
string _strFileName; //新文件名
string _strFileType; //文件类型
int _strFileSize; //文件大小 单位 KB
int _OldFileWidth; //原图宽度
int _OldFileHeight; //原图高度
int _NewImgHeight; //缩略图高度
int _NewImgWidth; //缩略图宽度
string _thumbImgPath; //缩略图完整地址
string _imagePath; //返回上传图片完整地址
string _errMsg; //返回出错提示
string _strFileFolder;
int _WatemarkPosX = 5;
int _WatemarkPosY = 5;
DateTime now = DateTime.Now; //取当前的时间到DateTime类的对象now中
public delegate void OnUploadEvent(object sender, EventArgs e);
public event OnUploadEvent OnUpload;
/// <summary>
/// 设置文件上传的目录
/// </summary>
public string strNewFileFolder
{
set { _strNewFileFolder = value; }
}
/// <summary>
/// 设置缩略图的尺寸
/// </summary>
public int NewImgpx
{
set { _NewImgpx = value; }
}
/// <summary>
/// 是否启用缩略图
/// </summary>
///
public bool IsThumb
{
set { _IsThumb = value; }
}
/// <summary>
/// 是否启用水印
/// </summary>
public bool IsWatermark
{
set { _IsWatermark = value; }
}
/// <summary>
/// 设置水印位置
/// </summary>
public int WatemarkPos
{
set { _WatemarkPos = value; }
}
/// <summary>
/// 设置水印类型 txt:文字水印 img:图片水印
/// </summary>
public string WatemarkType
{
set { _WatemarkType = value; }
}
/// <summary>
/// 设置水印文字
/// </summary>
public string WatemarkText
{
set { _WatemarkText = value; }
}
/// <summary>
/// 设置水印字体
/// </summary>
public string WatemarkFont
{
set { _WatemarkFont = value; }
}
/// <summary>
/// 设置水印字号
/// </summary>
public int WatemarkFontSize
{
set { _WatemarkFontSize = value; }
}
/// <summary>
/// 设置水印图片
/// </summary>
public string WatemarkImg
{
set { _WatemarkImg = value; }
}
/// <summary>
/// 设置上传文件的限制大小
/// </summary>
public int FileSizeMax
{
set { _FileSizeMax = value; }
}
/// <summary>
/// 上传文件原文件名
/// </summary>
public string srcFileName
{
get { return _srcFileName; }
}
/// <summary>
/// 扩展名
/// </summary>
public string srcFileExtension
{
get { return _srcFileExtension; }
}
/// <summary>
/// 新文件名
/// </summary>
public string strFileName
{
get { return _strFileName; }
}
/// <summary>
/// 文件类型
/// </summary>
public string strFileType
{
get { return _strFileType; }
}
/// <summary>
/// 文件大小 单位 KB
/// </summary>
public int strFileSize
{
get { return _strFileSize; }
}
/// <summary>
/// 原图宽度
/// </summary>
public int OldFileWidth
{
get { return _OldFileWidth; }
}
/// <summary>
/// 原图高度
/// </summary>
public int OldFileHeight
{
get { return _OldFileHeight; }
}
/// <summary>
/// 缩略图高度
/// </summary>
public int NewImgHeight
{
get { return _NewImgHeight; }
}
/// <summary>
/// 缩略图宽度
/// </summary>
public int NewImgWidth
{
get { return _NewImgWidth; }
}
/// <summary>
/// 缩略图完整地址
/// </summary>
public string thumbImgPath
{
get { return _thumbImgPath; }
}
/// <summary>
/// 返回上传图片完整地址
/// </summary>
public string ImagePath
{
get { return _imagePath; }
}
/// <summary>
/// 返回出错提示
/// </summary>
public string ErrMsg
{
get { return _errMsg; }
}
protected void Page_Load(object sender, EventArgs e)
{
_WatemarkImg = Server.MapPath("./") + _WatemarkImg; //水印图片
//文件上传的目录
_strNewFileFolder += now.Year.ToString() + "/";
_strFileFolder = Server.MapPath("./") + _strNewFileFolder;
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
//随机数
Random Rnd = new Random();
int strRnd = Rnd.Next(1, 99);
//新文件名称
string strFileName = now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + strRnd.ToString();
if (UpLoadFile.PostedFile.ContentLength != 0) //判断选取对话框的文件长度是否为0
{
//检测目录是否存在,不存在就建立目录
if (!Directory.Exists(_strFileFolder))
{
Directory.CreateDirectory(_strFileFolder);
}
//获取原文件信息
string srcFile = UpLoadFile.PostedFile.FileName;
string srcFileName = Path.GetFileName(srcFile.ToString()); //原文件名
string srcFileExtension = Path.GetExtension(srcFile.ToString()); //原文件扩展名
string strFileType = UpLoadFile.PostedFile.ContentType; //获得文件类型
int strFileSize = UpLoadFile.PostedFile.ContentLength / _FileSizeMax; //获得文件体积大小,单位KB
//限制文件上传大小
if (strFileSize > _FileSizeMax)
{
_errMsg = "上传文件超过限制,文件大小限制为:" + _FileSizeMax + " KB";
return;
}
strFileName = strFileName + srcFileExtension; //新文件名包括扩展名
_imagePath = _strNewFileFolder + strFileName; //返回上传后的图片路径
//输出
_srcFileName = srcFileName; //上传文件原文件名
_srcFileExtension = srcFileExtension; //扩展名
_strFileName = strFileName; //新文件名
_strFileType = strFileType; //文件类型
_strFileSize = strFileSize; //文件大小 单位 KB
//判断是否为图片
bool IsImg = true;
if (UpLoadFile.PostedFile.ContentType.IndexOf("image") < 0)
{
IsImg = false;
}
if (IsImg == false)
{
UpLoadFile.PostedFile.SaveAs(_strFileFolder + strFileName); //非图片文件上传
}
else
{
//加水印并上传
if (_IsWatermark == true)
{
System.Drawing.Image image;
if (strFileType == "image/bmp") //bmp图片格式
image = System.Drawing.Bitmap.FromStream(UpLoadFile.PostedFile.InputStream);
else
image = System.Drawing.Image.FromStream(UpLoadFile.PostedFile.InputStream);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
if (_WatemarkType == "txt") //文字水印
{
//将图片绘制到graphics中
g.DrawImage(image, 0, 0, image.Width, image.Height);
//设置文字的属性
System.Drawing.Font f = new System.Drawing.Font(_WatemarkFont, _WatemarkFontSize);
//判断图片的大小,如果图片过小,不写文字
if (image.Width < 200)
//在此设定在图片上所加的文字
_WatemarkText = "";
//写的文字的起始位置,x,y坐标
_WatemarkPosX = 5;
_WatemarkPosY = 5;
//设置字体的颜色
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.White);
//写字
g.DrawString(_WatemarkText, f, b, _WatemarkPosX, _WatemarkPosY);
//释放graphics
g.Dispose();
//保存加上水印的图片
image.Save(_strFileFolder + strFileName);
//释放image
image.Dispose();
}
else if (_WatemarkType == "img") //图片水印
{
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_WatemarkImg);
//水印放置左上角
if (_WatemarkPos == 1)
{
_WatemarkPosX = 5;
_WatemarkPosY = 5;
}
//水平居中垂直顶部
if (_WatemarkPos == 2)
{
_WatemarkPosX = (image.Width - copyImage.Width) / 2;
_WatemarkPosY = 5;
}
//右上角
if (_WatemarkPos == 3)
{
_WatemarkPosX = image.Width - copyImage.Width - 5;
_WatemarkPosY = 5;
}
//垂直居中水平靠左
if (_WatemarkPos == 4)
{
_WatemarkPosX = 5;
_WatemarkPosY = (image.Height - copyImage.Height) / 2;
}
//居中
if (_WatemarkPos == 5)
{
_WatemarkPosX = (image.Width - copyImage.Width) / 2;
_WatemarkPosY = (image.Height - copyImage.Height) / 2;
}
//垂直居中水平靠右
if (_WatemarkPos == 6)
{
_WatemarkPosX = image.Width - copyImage.Width - 5;
_WatemarkPosY = (image.Height - copyImage.Height) / 2;
}
//左下角
if (_WatemarkPos == 7)
{
_WatemarkPosX = 5;
_WatemarkPosY = image.Height - copyImage.Height - 5;
}
//水平居中垂直底部
if (_WatemarkPos == 8)
{
_WatemarkPosX = (image.Width - copyImage.Width) / 2;
_WatemarkPosY = image.Height - copyImage.Height - 5;
}
//水印放置右上角
if (_WatemarkPos == 9)
{
_WatemarkPosX = image.Width - copyImage.Width - 5;
_WatemarkPosY = image.Height - copyImage.Height - 5;
}
if (copyImage.Height < image.Height && copyImage.Width < image.Width)
{
//将水印打印到上传图片上去
g.DrawImage(copyImage, new System.Drawing.Rectangle(_WatemarkPosX, _WatemarkPosY, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
//释放graphics
g.Dispose();
}
//保存图片
image.Save(_strFileFolder + strFileName);
//释放image
image.Dispose();
}
}
else
{
UpLoadFile.PostedFile.SaveAs(_strFileFolder + strFileName); //不加水印上传
}
//获得图片尺寸
int OldFileHeight, OldFileWidth;
System.Drawing.Image img = System.Drawing.Image.FromStream(UpLoadFile.PostedFile.InputStream);
oldFileHeight = img.Height;
oldFileWidth = img.Width;
//创建缩略图
if (_IsThumb == true)
{
int NewImgHeight, NewImgWidth;
if (OldFileWidth > OldFileHeight)
{
NewImgWidth = _NewImgpx;
NewImgHeight = (int)(OldFileHeight / (float)OldFileWidth * _NewImgpx);
}
else
{
NewImgHeight = _NewImgpx;
NewImgWidth = (int)(OldFileWidth / (float)OldFileHeight * _NewImgpx);
}
//如果缩略图的尺寸大于原图尺寸,则用原图片大小
if (NewImgHeight >= OldFileHeight && NewImgWidth >= OldFileWidth)
{
NewImgHeight = OldFileHeight;
NewImgWidth = OldFileWidth;
}
System.Drawing.Image NewImg = img.GetThumbnailImage(NewImgWidth, NewImgHeight, null, System.IntPtr.Zero);
NewImg.Save(_strFileFolder + "Thumb_" + strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//返回数据
_NewImgHeight = NewImgHeight; //缩略图高度
_NewImgWidth = NewImgWidth; //缩略图宽度
_thumbImgPath = _strNewFileFolder + "Thumb_" + strFileName; //缩略图地址
}
//返回数据
_OldFileWidth = OldFileWidth; //原图宽度
_OldFileHeight = OldFileHeight; //原图高度
}
}
this.OnUpload(this, e);
}
}
08-24
08-24