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
}
}
新聞熱點
疑難解答
圖片精選