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

首頁(yè) > 編程 > .NET > 正文

在ASP.NET中上傳圖片并生成縮略圖的C#源碼

2024-07-10 12:55:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在asp.net中上傳圖片并生成縮略圖的c#源碼

 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.io;
  using system.drawing.imaging;
  
  namespace emeng.exam
  {
  /// <summary>
  /// thumbnail 的摘要說明。
  /// </summary>
  public class thumbnail : system.web.ui.page
  {
  protected system.web.ui.webcontrols.label label1;
  protected system.web.ui.webcontrols.button button1;
  
  private void page_load(object sender, system.eventargs e)
  {
  // 在此處放置用戶代碼以初始化頁(yè)面
  label1.text = "<h3>在asp.net里輕松實(shí)炙趼醞?lt;/h3>";
  button1.text = "上載并顯示縮略圖";
  
  }
  
  #region web 窗體設(shè)計(jì)器生成的代碼
  override protected void oninit(eventargs e)
  {
  //
  // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計(jì)器所必需的。
  //
  initializecomponent();
  base.oninit(e);
  }
  
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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)
  {
  httpfilecollection myfilecoll = httpcontext.current.request.files;
  httppostedfile mypostedfile = myfilecoll[0];
  
  if (mypostedfile.contenttype.tostring().tolower().indexof("image") < 0)
  {
  response.write("無(wú)效的圖形格式。");
  return;
  }
  getthumbnail(mypostedfile.filename, 100, 100,
  mypostedfile.contenttype.tostring(), false, mypostedfile.inputstream);
  }
  private system.drawing.imaging.imageformat getimagetype(object strcontenttype)
  {
  if ((strcontenttype.tostring().tolower()) == "image/pjpeg")
  {
  return system.drawing.imaging.imageformat.jpeg;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/gif")
  {
  return system.drawing.imaging.imageformat.gif;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/bmp")
  {
  return system.drawing.imaging.imageformat.bmp;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/tiff")
  {
  return system.drawing.imaging.imageformat.tiff;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/x-icon")
  {
  return system.drawing.imaging.imageformat.icon;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/x-png")
  {
  return system.drawing.imaging.imageformat.png;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/x-emf")
  {
  return system.drawing.imaging.imageformat.emf;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/x-exif")
  {
  return system.drawing.imaging.imageformat.exif;
  }
  else if ((strcontenttype.tostring().tolower()) == "image/x-wmf")
  {
  return system.drawing.imaging.imageformat.wmf;
  }
  else
  {
  return system.drawing.imaging.imageformat.memorybmp;
  }
  }
  
  private void getthumbnail(string strfilename, int iwidth, int iheight,
  string strcontenttype, bool blngetfromfile, system.io.stream imgstream)
  {
  system.drawing.image oimg;
  
  if (blngetfromfile)
  {
  oimg = system.drawing.image.fromfile(strfilename);
  }
  else
  {
  oimg = system.drawing.image.fromstream(imgstream);
  }
  oimg = oimg.getthumbnailimage(iwidth, iheight, null, intptr.zero);
  string strguid = system.guid.newguid().tostring().toupper();
  string strfileext = strfilename.substring(strfilename.lastindexof("."));
  response.contenttype = strcontenttype;
  memorystream memstream = new memorystream();
  oimg.save(memstream, getimagetype(strcontenttype));
  memstream.writeto(response.outputstream);
  }
  
  }
  } 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安陆市| 冕宁县| 临江市| 丽水市| 左云县| 池州市| 肥西县| 南郑县| 富源县| 四子王旗| 凉山| 滁州市| 昌宁县| 华容县| 上思县| 巍山| 大安市| 达日县| 汾阳市| 寻甸| 四平市| 城固县| 农安县| 霍林郭勒市| 东乡族自治县| 奈曼旗| 通州市| 时尚| 泽州县| 建昌县| 蓬安县| 宜兰县| 大同市| 萨迦县| 雷州市| 永济市| 鄄城县| 蒙自县| 清河县| 贵南县| 马龙县|