首页 > 编程 > .NET > 正文

在ASP.NET中上传图片并生成缩略图的C#源码

2024-07-10 12:55:11
字体:
来源:转载
供稿:网友
在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)
  {
  // 在此处放置用户代码以初始化页面
  label1.text = "<h3>在asp.net里轻松实炙趼酝?lt;/h3>";
  button1.text = "上载并显示缩略图";
  
  }
  
  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
  //
  // codegen: 该调用是 asp.net web 窗体设计器所必需的。
  //
  initializecomponent();
  base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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("无效的图形格式。");
  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);
  }
  
  }
  } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表