📄 post_pic.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using picmark;
using System.Data.SqlClient;
using System.IO;
namespace evpic
{
/// <summary>
/// 图片上传组件,可以按时间生成文件名,加水印图片,水印可以设置透明度,位置,e动力编写。
/// 可以到:www.eover.net/bbs/ 来交流
/// </summary>
public class post_pic : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Image imgID;
protected System.Web.UI.WebControls.Label Label1;
public static string mFileName;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//if(UploadFile.PostedFile.FileName.Trim()!="")
if (IsAllowedExtension(UploadFile))
{
//上传文件
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "/upfile/" + fileName + extension;
UploadFile.PostedFile.SaveAs(path);
//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
//System.Drawing.Image image = System.Drawing.Image.FromFile(path);
//Graphics g = Graphics.FromImage(image);
//g.DrawImage(image, 0, 0, image.Width, image.Height);
//Font f = new Font("Verdana", 32);
//Brush b = new SolidBrush(Color.Red);
//string addText = "ty158.com";
//g.DrawString(addText, f, b, 10, 10);
//g.Dispose();
picmark.ImageModification wm= new ImageModification();
wm.DrawedImagePath= Server.MapPath(".") + "/upfile/" + "backlogo.gif"; //水印图片
wm.ModifyImagePath=path; //图片的路径
wm.RightSpace=200; //水印位置
wm.BottoamSpace=64; //水银位置
wm.LucencyPercent=50; //透明度
wm.OutPath=Server.MapPath(".") + "/upfile/" + fileName + "_new" + extension;//生成的文件名
wm.DrawImage();
// System.Drawing.Image image = System.Drawing.Image.FromFile(path);
// System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/backlogo.gif");
//Graphics g = Graphics.FromImage(image);
//g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//g.Dispose();
//保存加水印过后的图片,删除原始图片
string newfile="upfile/" + fileName + "_new" + extension;
mFileName=fileName + "_new" + extension;
Label1.Text="图片上传成功,文件名为:"+mFileName;
//以下这句,是回传一下图片的地址到index.aspx的picurl文本控件
Response.Write ("<script>window.opener.document.myform.picurl.value='"+newfile +"'</script>");
if(File.Exists(path))
{
File.Delete(path);
}
}
}
/// <summary>
/// 这个过程可以来更新数据库
/// </summary>
void updata()
{
//这个过程可以更新的数据库,把图片地址什么的插入到数据库
}
#region IsAllowedExtension
/// <summary>
/// 是否允许该扩展名上传
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <returns>允许则返回true,否则返回false</returns>
public bool IsAllowedExtension(HtmlInputFile hifile)
{
string strOldFilePath = "",strExtension = "";
//允许上传的扩展名,可以改成从配置文件中读出
string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判断该扩展名是否合法
for(int i = 0; i< arrExtension.Length; i++)
{
if(strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -