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

首頁 > 編程 > .NET > 正文

用C#輕松在DOTNET中實現縮略圖

2024-07-21 02:17:42
字體:
來源:轉載
供稿:網友
菜鳥學堂:
 以前,在頁面上實現縮略圖必須借助第三方組件?,F在,有了.net,就可以很輕松地實現縮略圖。下面就是實現縮略圖的例子。

tothumbnailimage.aspx

<%@ page language="c#" codebehind="tothumbnailimage.aspx.cs" src="tothumbnailimage.aspx.cs" autoeventwireup="false"

inherits="exam_c.tothumbnailimage" %>
<html>
  <head>
    <title>lion互動網絡 =>生成縮略圖</title>
  </head>
  <body>
    <form id="form1" method="post" runat="server">
     </form>
  </body>
</html>

 tothumbnailimage.aspx.cs

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.drawing.imaging;
namespace exam_c
{
 /// <summary>
 /// tothumbnailimage 的摘要說明。
 /// </summary>
 public class tothumbnailimage : system.web.ui.page
 {
  /* 
  create by lion 
  2003-05-20 19:00 
  copyright (c) 2004 www.lionsky.net. all rights reserved.
  web: http://www.lionsky.net ;
  email: [email protected]
  */ 


  static hashtable htmimes=new hashtable();
  internal readonly string allowext = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
 
  #region web 窗體設計器生成的代碼
  override protected void oninit(eventargs e)
  {
   #region htmimes[".jpe"]="image/jpeg";
   htmimes[".jpeg"]="image/jpeg";
   htmimes[".jpg"]="image/jpeg";  
   htmimes[".png"]="image/png";  
   htmimes[".tif"]="image/tiff";
   htmimes[".tiff"]="image/tiff";
   htmimes[".bmp"]="image/bmp";
   #endregion
   //調用生成縮略圖方法
   tothumbnailimages("lionsky.jpg","b.gif",300);
  } 
  #endregion

  #region helper
 
  /// <summary>
  /// 獲取圖像編碼解碼器的所有相關信息
  /// </summary>
  /// <param name="mimetype">包含編碼解碼器的多用途網際郵件擴充協議 (mime) 類型的字符串</param>
  /// <returns>返回圖像編碼解碼器的所有相關信息</returns>
  static imagecodecinfo getcodecinfo(string mimetype)
  {
   imagecodecinfo[] codecinfo = imagecodecinfo.getimageencoders();
   foreach(imagecodecinfo ici in codecinfo)
   {
    if(ici.mimetype == mimetype)return ici;
   }
   return null;
  }

  /// <summary>
  /// 檢測擴展名的有效性
  /// </summary>
  /// <param name="sext">文件名擴展名</param>
  /// <returns>如果擴展名有效,返回true,否則返回false.</returns>
  bool checkvalidext(string sext)
  {  
   bool flag=false;
   string[] aext = allowext.split('|');
   foreach(string filetype in aext)
   {
    if(filetype.tolower()==sext)
    {
     flag = true;
     break;
    }
   }  
   return flag;
  }

  /// <summary>
  /// 保存圖片
  /// </summary>
  /// <param name="image">image 對象</param>
  /// <param name="savepath">保存路徑</param>
  /// <param name="ici">指定格式的編解碼參數</param>
  void saveimage(system.drawing.image image,string savepath,imagecodecinfo ici)
  {
   //設置 原圖片 對象的 encoderparameters 對象
   encoderparameters parameters = new encoderparameters(1);
   parameters.param[0] = new encoderparameter(encoder.quality, ((long) 90));
   image.save(savepath, ici, parameters);
   parameters.dispose();
  }
  #endregion

  #region methods

  /// <summary>
  /// 生成縮略圖
  /// </summary>
  /// <param name="sourceimagepath">原圖片路徑(相對路徑)</param>
  /// <param name="thumbnailimagepath">生成的縮略圖路徑,如果為空則保存為原圖片路徑(相對路徑)</param>
  /// <param name="thumbnailimagewidth">縮略圖的寬度(高度與按源圖片比例自動生成)</param>
  public void tothumbnailimages(string sourceimagepath,string thumbnailimagepath,int thumbnailimagewidth)
  {
   string sourceimagepath = sourceimagepath;
   string thumbnailimagepath = thumbnailimagepath;
   int thumbnailimagewidth = thumbnailimagewidth;
   string sext = sourceimagepath.substring(sourceimagepath.lastindexof(".")).tolower();
   if(sourceimagepath.tostring()==system.string.empty) throw new nullreferenceexception("sourceimagepath

is null!");
   if(!checkvalidext(sext))
   {
    throw new argumentexception("原圖片文件格式不正確,支持的格式有[ "+ allowext +"

]","sourceimagepath");
   }
   //從 原圖片 創建 image 對象
   system.drawing.image image = system.drawing.image.fromfile(httpcontext.current.server.mappath

(sourceimagepath)); 
   int num = ((thumbnailimagewidth / 4) * 3);
   int width = image.width;
   int height = image.height;
   //計算圖片的比例
   if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
   {
    num = ((height * thumbnailimagewidth) / width);
   }
   else
   {
    thumbnailimagewidth = ((width * num) / height);
   }
   if ((thumbnailimagewidth < 1) || (num < 1))
   {
    return;
   }
   //用指定的大小和格式初始化 bitmap 類的新實例
   bitmap bitmap = new bitmap(thumbnailimagewidth, num, pixelformat.format32bppargb);
   //從指定的 image 對象創建新 graphics 對象
   graphics graphics = graphics.fromimage(bitmap);
   //清除整個繪圖面并以透明背景色填充
   graphics.clear(color.transparent);
   //在指定位置并且按指定大小繪制 原圖片 對象
   graphics.drawimage(image, new rectangle(0, 0, thumbnailimagewidth, num));
   image.dispose();   
   try
   {  
    //將此 原圖片 以指定格式并用指定的編解碼參數保存到指定文件
    string savepath = (thumbnailimagepath==null?sourceimagepath:thumbnailimagepath); 
    saveimage(bitmap,httpcontext.current.server.mappath(savepath),getcodecinfo((string)htmimes

[sext]));
   }
   catch(system.exception e)
   {
    throw e;
   }
   finally
   {
    bitmap.dispose();     
    graphics.dispose();
   }
  }
  #endregion

 }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰考县| 阳谷县| 江达县| 体育| 朔州市| 西吉县| 襄垣县| 江陵县| 淮南市| 东阿县| 广南县| 永福县| 凤庆县| 读书| 夏邑县| 岑溪市| 宜良县| 鄱阳县| 井研县| 弋阳县| 广平县| 肃宁县| 瑞昌市| 黄陵县| 邹平县| 庄河市| 墨玉县| 修武县| 沙洋县| 长岛县| 永年县| 射洪县| 富阳市| 萍乡市| 津市市| 天台县| 宁蒗| 阳信县| 江口县| 巩义市| 台安县|