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

首頁 > 編程 > .NET > 正文

asp.net圖片上傳生成縮略圖的注意事項

2024-07-10 13:22:34
字體:
來源:轉載
供稿:網友
bitmap.Save(imgPath,ImageFormat.Jpeg);  
//這是保存縮略圖的一段代碼,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本來就是jpg格式的,也不能去掉。因為如果去掉的話,生成的縮略圖比原始圖片還要大!


//另外,imgPath必須首先創建,否則會產生GDI+的一般性錯誤。
path=System.Web.HttpContext.Current.Server.MapPath(path);
使用if(!System.IO.Directory.Exists(path))System.IO.Directiory.CreateDirectory(path); 


//生成縮略圖,不要使用GetThumbnailImage方法,這個方法產生的縮略圖質量奇差無比,不能使用!



//簡單代碼如下:
     string path=System.Web.HttpContext.Current.Server.MapPath(strpath);
      sourcePath=System.Web.HttpContext.Current.Server.MapPath(sourcePath);

      if(!System.IO.Directory.Exists(path))System.IO.Directory.CreateDirectory(path);

      string sourceImage =sourcePath + fileName;
      string thumbImage = path + fileName;

     //原圖(引用)
   Image img=Image.FromFile(sourceImage,true);

   //實際縮略圖大小
   System.Drawing.Size size=Pic.ImgSize(maxW,maxH,img.Width,img.Height);
   int w=size.Width;
   int h=size.Height;

   //繪制縮略圖
   Bitmap bitmap=new Bitmap(w,h);
   Graphics g=Graphics.FromImage(bitmap);

   //設定縮略圖呈現質量
   g.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
   g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

   //清潔背景
   g.Clear(Color.White);

      Rectangle thumbRect=new Rectangle(0,0,w,h);
      g.DrawImage(img,thumbRect);

      //保存縮略圖
   bitmap.Save(thumbImage,ImageFormat.Jpeg);

   //釋放內存
   bitmap.Dispose();
   img.Dispose();
   g.Dispose();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜梁县| 于都县| 西林县| 辛集市| 哈尔滨市| 通河县| 北辰区| 阳原县| 巴东县| 博湖县| 磴口县| 溧水县| 徐汇区| 台东县| 威信县| 蓬安县| 资溪县| 尚志市| 西宁市| 甘泉县| 阿克| 大英县| 大石桥市| 富平县| 徐汇区| 佳木斯市| 呼和浩特市| 泰安市| 盘锦市| 长治县| 嫩江县| 松滋市| 株洲县| 临城县| 临漳县| 盐源县| 合肥市| 绵阳市| 双柏县| 健康| 多伦县|