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

首頁 > 編程 > .NET > 正文

代碼實例:在ASP.NET中上傳圖片并生成縮略圖

2024-07-10 13:08:45
字體:
來源:轉載
供稿:網友
private void btnuploadpicture_click(object sender, system.eventargs e)
{
   //檢查上傳文件的格式是否有效
   if(this.uploadfile.postedfile.contenttype.tolower().indexof("image") < 0)
   {
    response.write("上傳圖片格式無效!");
    return;
   }

   //生成原圖
   byte[] ofilebyte = new byte[this.uploadfile.postedfile.contentlength];
   system.io.stream ostream = this.uploadfile.postedfile.inputstream;
   system.drawing.image oimage = system.drawing.image.fromstream(ostream);

   int owidth = oimage.width; //原圖寬度
   int oheight = oimage.height; //原圖高度
   int twidth = 100; //設置縮略圖初始寬度
   int theight = 100; //設置縮略圖初始高度

   //按比例計算出縮略圖的寬度和高度
   if(owidth >= oheight)
   {
    theight = (int)math.floor(convert.todouble(oheight) * (convert.todouble(twidth) / convert.todouble(owidth)));
   }
   else
   {
    twidth = (int)math.floor(convert.todouble(owidth) * (convert.todouble(theight) / convert.todouble(oheight)));
   }

   //生成縮略原圖
   bitmap timage = new bitmap(twidth,theight);
   graphics g = graphics.fromimage(timage);
   g.interpolationmode = system.drawing.drawing2d.interpolationmode.high; //設置高質量插值法
   g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;//設置高質量,低速度呈現平滑程度
   g.clear(color.transparent); //清空畫布并以透明背景色填充
   g.drawimage(oimage,new rectangle(0,0,twidth,theight),new rectangle(0,0,owidth,oheight),graphicsunit.pixel);

   string ofullname = server.mappath(".") + "/" + "o" + datetime.now.toshortdatestring().replace("-","") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.second.tostring() + datetime.now.millisecond.tostring() + ".jpg"; //保存原圖的物理路徑
   string tfullname = server.mappath(".") + "/" + "t" + datetime.now.toshortdatestring().replace("-","") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.second.tostring() + datetime.now.millisecond.tostring() + ".jpg"; //保存縮略圖的物理路徑

   try
   {
    //以jpg格式保存圖片
    oimage.save(ofullname,system.drawing.imaging.imageformat.jpeg);
    timage.save(tfullname,system.drawing.imaging.imageformat.jpeg);
   }
   catch(exception ex)
   {
    throw ex;
   }
   finally
   {
    //釋放資源
    oimage.dispose();
    g.dispose();
    timage.dispose();
   }
}  
國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵溪市| 江孜县| 通州市| 乌兰察布市| 肥东县| 五常市| 双桥区| 司法| 渭源县| 浦东新区| 富蕴县| 澄江县| 达拉特旗| 江陵县| 黄山市| 平远县| 澜沧| 抚宁县| 兴安盟| 黎川县| 秦安县| 马龙县| 清涧县| 舟山市| 宿州市| 宜兴市| 武山县| 融水| 阿拉善盟| 巴楚县| 肇东市| 建水县| 仙居县| 石林| 长垣县| 北碚区| 宣城市| 大庆市| 宁河县| 亳州市| 鄂尔多斯市|