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

首頁 > 編程 > .NET > 正文

asp.net中生在文章縮略圖并加入圖片信息

2024-07-10 13:06:12
字體:
來源:轉載
供稿:網友

asp.net中生在文章縮略圖,并在圖片上加入圖片版板等信息


 //定義image類的對象
     drawing.image image,newimage;
  //圖片路徑
  protected string imagepath;
  //圖片類型
  protected string imagetype;
  //圖片名稱
  protected string imagename;

  //提供一個回調方法,用于確定image對象在執行生成縮略圖操作時何時提前取消執行
  //如果此方法確定 getthumbnailimage 方法應提前停止執行,則返回 true;否則返回 false
  system.drawing.image.getthumbnailimageabort callb = null;

  private void sm_click(object sender, system.eventargs e)
  {
   string mpath;

   if("" != file1.postedfile.filename)  //file1為上傳文件控件
   {
    imagepath = file1.postedfile.filename;
    //取得圖片類型
    imagetype= imagepath.substring(imagepath.lastindexof(".")+1);
    //取得圖片名稱
    imagename = imagepath.substring(imagepath.lastindexof("http://")+1);
    //判斷是否是jpg或者gif圖片,這里只是舉個例子,并不一定必須是這兩種圖片
    if("jpg" != imagetype && "gif" != imagetype)
    {
     response.write("<script language='javascript'> alert('對不起!請您選擇jpg或者gif格式的圖片!');</script>");
     return;
    }
    else
    {
     try
     {
      //建立虛擬路徑
      mpath=server.mappath("uploadfiles");
      //保存到虛擬路徑
      file1.postedfile.saveas(mpath+"http://"+imagename);

      //顯示原圖, imagesource為圖片控件
      //imagesource.imageurl = "uploadfiles/"+imagename;

      //為上傳的圖片建立引用
      image=system.drawing.image.fromfile(mpath+"http://"+imagename);  
      //生成縮略圖
      newimage=image.getthumbnailimage(200,200,callb,new system.intptr());
      //把縮略圖保存到指定的虛擬路徑
      newimage.save(server.mappath("uploadfiles")+"http://small"+imagename);
      //釋放image對象占用的資源
      image.dispose();
      //釋放newimage對象的資源
      newimage.dispose();
      //顯示縮略圖

      addtexttoimg ("uploadfiles/"+"small"+imagename,"pic info");   // 在圖片上加入信息說明
      image1.imageurl = "uploadfiles/"+"small"+imagename;

      script.alert("上傳成功!");
     }
     catch
     {
      script.alert("上傳失敗!");
     }
    
    } // end else
   }

// 在圖片上加入自己的信息,
  // addtexttoimg (physicpath,"pic info");
  private void addtexttoimg(string filename,string text)
  {
   //string sss = mappath(filename);

   if ( !file.exists ( filename))   {
    throw new filenotfoundexception("the file don't exist!");
   }

   //還需要判斷文件類型是否為圖像類型,這里就不贅述了

   system.drawing.image image = system.drawing.image.fromfile(filename);//mappath(filename));
   bitmap bitmap = new bitmap(image,image.width,image.height);
   graphics g = graphics.fromimage(bitmap);

   float fontsize = 22.0f; //字體大小
   float textwidth = text.length*fontsize; //文本的長度
   //下面定義一個矩形區域,以后在這個矩形里畫上白底黑字
   float rectx = 0;
   float recty = 0;
   float rectwidth = text.length*(fontsize+18);
   float rectheight = fontsize+18;
   //聲明矩形域
   rectanglef textarea = new rectanglef(rectx,recty,rectwidth,rectheight);
   font font = new font("宋體",fontsize);//定義字體
   brush whitebrush = new solidbrush(color.white);
   brush blackbrush = new solidbrush(color.black);
   g.fillrectangle(blackbrush,rectx,recty,rectwidth,rectheight);
   g.drawstring(text,font,whitebrush,textarea);
   memorystream ms = new memorystream();
   //保存為jpg類型
   bitmap.save(ms,imageformat.jpeg);

   //輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁面中了
 /*  response.clear();
   response.contenttype = "image/jpeg";
   response.binarywrite( ms.toarray() );
  */
   filestream fs=new filestream(filename, filemode.openorcreate);//.createnew);
   fs.write(ms.toarray(),0,ms.toarray().length);
   fs.close();

   image1.imageurl = filename;  // 將圖片顯示在image控件中
   g.dispose();
   bitmap.dispose();
   image.dispose();
  }


 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 讷河市| 抚宁县| 玛曲县| 壤塘县| 姜堰市| 康平县| 舟曲县| 象州县| 五大连池市| 仁布县| 本溪市| 新安县| 龙井市| 江永县| 米易县| 德昌县| 团风县| 松溪县| 吕梁市| 昆明市| 阿坝县| 当涂县| 红河县| 满洲里市| 雷州市| 巧家县| 山阳县| 景宁| 延庆县| 策勒县| 化德县| 洛阳市| 宁城县| 贡嘎县| 平和县| 五寨县| 胶南市| 淮北市| 佛冈县| 茂名市| 松滋市|