關鍵字:C#
實例下載:
ToThumbnailImage
<%@ Page language=
<html>
<head>
<title>Lion互動網絡 =>生成縮略圖</title>
</head>
<body>
<form id=
</form>
</body>
</html>
ToThumbnailImage
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace Exam_C
{
/// <summary>
/// ToThumbnailImage 的摘要說明
/// </summary>
public class ToThumbnailImage : System
{
/*
Create By lion
Copyright (C)
Web: ;
Email:
*/
static Hashtable htmimes=new Hashtable();
internal readonly string AllowExt =
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
#region htmimes[
htmimes[
htmimes[
htmimes[
htmimes[
htmimes[
htmimes[
#endregion
//調用生成縮略圖方法
ToThumbnailImages(
}
#endregion
#region Helper
/// <summary>
/// 獲取圖像編碼解碼器的所有相關信息
/// </summary>
/// <param name=
/// <returns>返回圖像編碼解碼器的所有相關信息</returns>
static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo
foreach(ImageCodecInfo ici in CodecInfo)
{
if(ici
}
return null;
}
/// <summary>
/// 檢測擴展名的有效性
/// </summary>
/// <param name=
/// <returns>如果擴展名有效
bool CheckValidExt(string sExt)
{
bool flag=false;
string[] aExt = AllowExt
foreach(string filetype in aExt)
{
if(filetype
{
flag = true;
break;
}
}
return flag;
}
/// <summary>
/// 保存圖片
/// </summary>
/// <param name=
/// <param name=
/// <param name=
void SaveImage(System
{
//設置 原圖片 對象的 EncoderParameters 對象
EncoderParameters parameters = new EncoderParameters(
parameters
image
parameters
}
#endregion
#region Methods
/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public void ToThumbnailImages(string sourceImagePath
{
string SourceImagePath = sourceImagePath;
string ThumbnailImagePath = thumbnailImagePath;
int ThumbnailImageWidth = thumbnailImageWidth;
string sExt = SourceImagePath
if(SourceImagePath
if(!CheckValidExt(sExt))
{
throw new ArgumentException(
}
//從 原圖片 創建 Image 對象
System
int num = ((ThumbnailImageWidth /
int width = image
int height = image
//計算圖片的比例
if ((((double) width) / ((double) height)) >=
{
num = ((height * ThumbnailImageWidth) / width);
}
else
{
ThumbnailImageWidth = ((width * num) / height);
}
if ((ThumbnailImageWidth <
{
return;
}
//用指定的大小和格式初始化 Bitmap 類的新實例
Bitmap bitmap = new Bitmap(ThumbnailImageWidth
//從指定的 Image 對象創建新 Graphics 對象
Graphics graphics = Graphics
//清除整個繪圖面並以透明背景色填充
graphics
//在指定位置並且按指定大小繪制 原圖片 對象
graphics
image
try
{
//將此 原圖片 以指定格式並用指定的編解碼參數保存到指定文件
string savepath = (ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
SaveImage(bitmap
}
catch(System
{
throw e;
}
finally
{
bitmap
graphics
}
}
#endregion
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12672.html