把上传功能做到一个页面里,在需要上传的页面直接通过iframe调用即可:
<!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>
<title></title>
</head>
<body style="margin:0; padding:0;">
<form id="form1" name="form1">
<div style="float:left;">图片:<input id="input1" name="input1" type="text" /> </div> <div style="float:left;"><iframe id="ifream1" src="upfile.aspx?id=input1" width="400px" height="30px" scrolling="no" frameborder="0"></iframe></div>
</form></body>
</html>
上传页面代码:upfile.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="admin_upfile" %>
<!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 id="Head1" runat="server">
<title>文件上传</title>
<link rel="stylesheet" href="images/CSS.css" type="text/css" />
</head>
<body style="background-color:#F6F6F6; margin-top:0px; margin-left:0px" >
<form id="form2" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Height="22px" Width="185px" />
<asp:CheckBox ID="CheckBox1" runat="server" Text="添加水印" />
<asp:Button ID="Button1" runat="server" Text="上传图片" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
原理可以小了解一下:首先载入已经上传的图片,以这个图片作为背景创建一个画布(bitmap),然后在画布上创建一个绘画层(Graphics),再在层上画出指定的字,颜色,和位置都可以调. 字画好再生成一个新的随即名称,然后把刚才画好的内容保存到新名称的图片里,最后再删除掉原图片. 并返回最新图片的路径给调用页面OK!
upfile.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class admin_upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
{
Response.Write("<script>alert(\"参数错误!\");location.href='upfile.aspx';</script>");
Response.End();
}
if (FileUpload1.PostedFile != null)
{
string filename;//文件名字
string newname;
string Newpathurl;
filename = FileUpload1.PostedFile.FileName;
string exten = Path.GetExtension(filename).ToLower();//扩展名
if (exten != ".gif" && exten != ".jpg" && exten != ".jpeg" && exten != ".bmp") //判断文件类型
{
Response.Write("<script>alert('图片只支持--gif|jpg|jpeg|bmp');location.href='upfile.aspx';</script>");
Response.End();
}
newname = sjname() + "." + exten;//新名称
string pathurl;
pathurl = Server.MapPath("~") + "\\UploadFile\\" + newname; //文件保存路径
string kb = FileUpload1.FileContent.Length.ToString(); //文件大小 单位:字节
FileUpload1.PostedFile.SaveAs(pathurl);
if (CheckBox1.Checked)//若勾上了添加水印
{
try
{
using (System.Drawing.Bitmap bm = new System.Drawing.Bitmap(pathurl))//创建一张图片作为背景的画布
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm)) //在画布上创建一个层
{
g.DrawString("图片来源(www.xiaojiang-design.com)", new System.Drawing.Font("宋体", 15), System.Drawing.Brushes.Yellow,bm.Width-350, bm.Height-50);//画上字
}
newname = sjname() + "." + exten;//新名称
Newpathurl = Server.MapPath("~") + "\\UploadFile\\" + newname; //文件保存路径
bm.Save(Newpathurl);//保存添加了水印的图片
bm.Dispose();
}
if (File.Exists(pathurl))
{
File.Delete(pathurl);//删除原图
}
}
catch
{
}
}
Response.Write("<script>parent.document.form1." + Request.QueryString["id"].ToString() + ".value='/Uploadfile/" + newname + "';</script>"); //把上传成功后的新文件路径传递给其父页面的传递过来的控件
}
}
/// <summary>
/// 产生个随即名称
/// </summary>
/// <returns></returns>
public string sjname()
{
string sj = null;
sj = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.TimeOfDay.Hours.ToString() + DateTime.Now.TimeOfDay.Minutes.ToString() + DateTime.Now.TimeOfDay.Milliseconds.ToString();
return sj;
}
}