国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 常州市| 莒南县| 威远县| 九寨沟县| 永修县| 云安县| 林周县| 徐汇区| 武山县| 句容市| 公安县| 吴堡县| 普定县| 丰顺县| 监利县| 平谷区| 江达县| 尚志市| 莎车县| 沈阳市| 锦屏县| 济宁市| 安国市| 紫金县| 芦溪县| 云和县| 门源| 南雄市| 博客| 新巴尔虎左旗| 遂昌县| 江阴市| 延津县| 轮台县| 德令哈市| 正定县| 永仁县| 大渡口区| 承德市| 盈江县| 湘阴县|