熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net生成縮略圖及給原始圖加水印

2022-06-13   來源: .NET編程 
生成縮略圖及給原始圖加水印代碼如下

     using SystemIO;
  using SystemDrawingImaging;
  private void Button_ServerClick(object sender SystemEventArgs e)
  {
  Graphics g=null;
  SystemDrawingImage upimage=null;
  SystemDrawingImage thumimg=null;
  SystemDrawingImage simage=null;
  Bitmap outputfile=null;
  try
  {
  string extension = PathGetExtension(FilePostedFileFileName)ToUpper();
  string filename = DateTimeNowToString(yyyyMMddhhmmss);
  string smallpath = ServerMapPath()+/smallimg/;
  string bigpath = ServerMapPath()+/bigimg/;
  int widthheightnewwidthnewheight;
  SystemDrawingImageGetThumbnailImageAbort callb =new SystemDrawingImageGetThumbnailImageAbort(ThumbnailCallback);
  if(!DirectoryExists(smallpath))
  DirectoryCreateDirectory(smallpath);
  if(!DirectoryExists(bigpath))
  DirectoryCreateDirectory(bigpath);
  Stream upimgfile = FilePostedFileInputStream;
  string simagefile = ServerMapPath(alogojpg); //要加水印的文件
  simage=SystemDrawingImageFromFile(simagefile);
  upimage= SystemDrawingImageFromStream(upimgfile); //上傳的圖片
  width = upimageWidth;
  height = upimageHeight;
  if(width>height)
  {
  newwidth=;
  newheight =(int)((double)height/(double)width * (double)newwidth);
  }
  else
  {
  newheight=;
  newwidth=(int)((double)width/(double)height * (double)newheight);
  }
  thumimg = upimageGetThumbnailImage(newwidthnewheightcallbIntPtrZero);
  outputfile=new Bitmap(upimage);
  g=GraphicsFromImage(outputfile);
  gDrawImage(simagenew Rectangle(upimageWidthsimageWidthupimageHeightsimageHeightupimageWidthupimageHeight)upimageWidthupimageHeightGraphicsUnitPixel);
  string newpath = bigpath + filename + extension; //原始圖路徑
  string thumpath = smallpath + filename + extension; //縮略圖路徑
  outputfileSave(newpath);
  thumimgSave(thumpath);
  outputfileDispose();
  }
  catch(Exception ex)
  {
  throw ex;
  }
  finally
  {
  if(g!=null)
  gDispose();
  if(thumimg!=null)
  thumimgDispose();
  if(upimage!=null)
  upimageDispose();
  if(simage!=null)
  simageDispose();
  }
  }
  public bool ThumbnailCallback()
  {
  return false;
  }


From:http://tw.wingwit.com/Article/program/net/201311/11730.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.