国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#圖片處理

2024-07-21 02:26:03
字體:
來源:轉載
供稿:網友
<%@   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()+"<br>");  
  strmsg.append("客戶端文件地址:"+postedfile.filename+"<br>");  
  strmsg.append("上傳文件的文件名:"+filename+"<br>");  
  strmsg.append("上傳文件的擴展名:"+fileextension+"<br>");  
  strmsg.append("上傳文件的大小:"+postedfile.contentlength+"<br>");  
   
   
  //按當前時間(精確到毫秒)給文件命名,不取中文名,避免編碼錯誤而無法下載或顯示  
  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()+"<br>");  
  strmsg.append("上傳圖片的寬度:"+image.width.tostring()+"<br>");  
  //如果上傳的圖片超過了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+"http://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+"http://")+pys_filename+fileextension);  
  //釋放newimage對象占用的資源  
  newimage.dispose();  
  strmsg.append("上傳的圖片大小超過了100kb,為了不影響撥號上網單位的瀏覽速度,已對該圖生成縮略圖。<br>");  
  }  
  //釋放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); 


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通榆县| 稻城县| 崇左市| 鸡东县| 湾仔区| 镇沅| 东宁县| 元谋县| 锡林浩特市| 油尖旺区| 沾化县| 泽普县| 定南县| 同德县| 天门市| 华蓥市| 晋州市| 蕲春县| 铁力市| 二连浩特市| 临湘市| 大方县| 蒙自县| 扎赉特旗| 六枝特区| 长春市| 古蔺县| 天门市| 廊坊市| 云安县| 洛阳市| 洞口县| 江门市| 高州市| 耿马| 玛曲县| 桐庐县| 马鞍山市| 鸡东县| 潮州市| 扬州市|