<% Page Debug ="true" Language="c#" %>
<% Import Namespace="System.IO" %>
<% Import Namespace="System.Data" %>
<% Import Namespace="System.Web" %>
<% Import Namespace="System.Data.SqlClient" %>
<html>
<!--此程序由长沙铁路公安处彭永爽开发,版权所有,不得复制-->
<head>
<script language="c#" runat="server">
//页面载入事件
public void Page_Load(object sender,System.EventArgs e)
{
//定义图片标志
string pys_pic;
pys_pic="";
//定义文件夹名称
string pys_dir;
pys_dir=DateTime.Now.Year.ToString();
//定义文件名前部分,以免上传文件因文件名重复而覆盖
string pys_filename;
//如果客户端已经上传文件,则保存
if (!IsPostBack)
{
LiuXi.Text="等待上传";
}
else
{
//检查是否有文章id,如果没有,则处理
if (Request["pysid"]==null | Request["pysid"]=="")
{
strStatus.Text="<font color=red>发生错误,您没有从正当途径进入该页面,请关闭本页面!</font>";
}
else
{
//遍历File表单元素
System.Web.HttpFileCollection files=System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg=new System.Text.StringBuilder("<font color=red>上传的文件信息分别为:</font><hr size=1 color=blue>");
int fileCount;
int filecount=files.Count;
//储存上传图片的客户端地址,以便检测上传图片的大小
string imgPath;
//检查上传文件是否图片
string pys_type;
//保存图片生成缩略图的宽度
int pys_width;
try
{
//数据库连接字符串
string ConnectionString="server=(local);database=csc;uid=forcool;pwd=";
string Sql="select * from upfile";
//创建SqlConnection对象
SqlConnection thisConnection=new SqlConnection(ConnectionString);
//创建DataAdaper对象并初始化
SqlDataAdapter adapter=new SqlDataAdapter(Sql,thisConnection);
//创建DataSet对象
DataSet data=new DataSet();
//创建SqlCommandBuilder对象,并和SqlDataAdapter关联
SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
adapter.Fill(data,"upfile");
for(fileCount=0;fileCount<files.Count;fileCount++)
{
//定义访问客户端上传文件的个数
System.Web.HttpPostedFile postedFile=files[fileCount];
string fileName,fileExtension;
//取得上传的文件名
fileName=System.IO.Path.GetFileName(postedFile.FileName);
if (fileName!=String.Empty)
{
//取得文件扩展名
fileExtension=System.IO.Path.GetExtension(fileName);
//上传的文件信息
strMsg.Append("<font color=red><b>"+(fileCount+1)+".</b></font>");
strMsg.Append("上传的文件类型:"+postedFile.ContentType.ToString()+"
");
strMsg.Append("客户端文件地址:"+postedFile.FileName+"
");
strMsg.Append("上传文件的文件名:"+fileName+"
");
strMsg.Append("上传文件的扩展名:"+fileExtension+"
");
strMsg.Append("上传文件的大小:"+postedFile.ContentLength+"
");
//按当前时间(精确到毫秒)给文件命名,不取中文名,避免编码错误而无法下载或显示
pys_filename=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
//创建随机数对象
Random rnd=new Random();
//调用Next方法产生随机数
pys_filename+=rnd.Next(1,100000).ToString();
pys_filename+="-";
pys_filename+=fileCount;
pys_filename=Request.QueryString["pysid"]+"-"+pys_filename;
pys_type=fileExtension;
pys_type=pys_type.ToLower();
//给缩略图宽度赋初值
pys_width=0;
//如果是图片则检查大小,如果图片过大则生成缩略图
if (pys_type==".jpg" | pys_type==".gif" | pys_type==".bmp" | pys_type==".png")
{
pys_pic="是";
//取得上传图片的路径
imgPath=postedFile.FileName;
//针对选择的图片建立Image对象
System.Drawing.Image image=System.Drawing.Image.FromFile(imgPath);
//使用Image对象的Height and Width属性取得图片的高度和宽度
strMsg.Append("上传图片的高度:"+image.Height.ToString()+"
");
strMsg.Append("上传图片的宽度:"+image.Width.ToString()+"
");
//如果上传的图片超过了100KB
if (postedFile.ContentLength>100000 || image.Width>400)
{
//生成缩略图
System.Drawing.Image.GetThumbnailImageAbort callb=null;
pys_width=(image.Height)*200/(image.Width);
System.Drawing.Image newimage=image.GetThumbnailImage(200,pys_width,callb,new System.IntPtr());
//保存缩略图
newimage.Save(Server.MapPath(""+pys_dir+"\\small-")+pys_filename+fileExtension);
//释放newimage对象占用的资源
//对图形压缩处理
pys_width=(image.Height)*400/(image.Width);
newimage=image.GetThumbnailImage(400,pys_width,callb,new System.IntPtr());
//保存压缩图
newimage.Save(Server.MapPath(""+pys_dir+"\\")+pys_filename+fileExtension);
//释放newimage对象占用的资源
newimage.Dispose();
strMsg.Append("上传的图片大小超过了100KB,为了不影响拨号上网单位的浏览速度,已对该图生成缩略图。
");
}
//释放image占用的资源
image.Dispose();
}
strMsg.Append("<hr size=1 color=blue>");
//在DataSet中添加上传的文件信息
DataRow newRow=data.Tables["upfile"].NewRow();
newRow["pysid"]=Request["pysid"];
newRow["文件名称"]=fileName;
newRow["文件类型"]=fileExtension;
newRow["文件大小"]=postedFile.ContentLength;
newRow["文件位置"]=pys_dir+"/"+pys_filename+fileExtension;
if (pys_width>0)
{
newRow["缩略图位置"]=pys_dir+"/small-"+pys_filename+fileExtension;
}
data.Tables["upfile"].Rows.Add(newRow);
C#图片处理
80酷酷网 80kuku.com