熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2013-11-13 10:08:26  來源: .NET編程 
以前在頁面上實現縮略圖必須借助第三方組件現在有了NET就可以很輕松地實現縮略圖下面就是實現縮略圖的例子

  關鍵字:C# ASPNET縮略圖

  實例下載:

ToThumbnailImageaspx


<%@ Page language=c# Codebehind=ToThumbnailImageaspxcs Src=ToThumbnailImageaspxcs AutoEventWireup=false Inherits=Exam_CToThumbnailImage %>
<html>
<head>
<title>Lion互動網絡 =>生成縮略圖</title>
</head>
<body>
<form id=Form method=post runat=server>
</form>
</body>
</html>



ToThumbnailImageaspxcs

using System;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemWeb;
using SystemWebSessionState;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIHtmlControls;
using SystemDrawingImaging;
namespace Exam_C
{
/// <summary>
/// ToThumbnailImage 的摘要說明
/// </summary>
public class ToThumbnailImage : SystemWebUIPage
{
/*
Create By lion
:
Copyright (C) wwwLionSkyNet All rights reserved
Web: ;
Email:
*/


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(lionskyjpgbgif);
}
#endregion

#region Helper

/// <summary>
/// 獲取圖像編碼解碼器的所有相關信息
/// </summary>
/// <param name=mimeType>包含編碼解碼器的多用途網際郵件擴充協議 (MIME) 類型的字符串</param>
/// <returns>返回圖像編碼解碼器的所有相關信息</returns>
static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfoGetImageEncoders();
foreach(ImageCodecInfo ici in CodecInfo)
{
if(iciMimeType == mimeType)return ici;
}
return null;
}

/// <summary>
/// 檢測擴展名的有效性
/// </summary>
/// <param name=sExt>文件名擴展名</param>
/// <returns>如果擴展名有效返回true否則返回false</returns>
bool CheckValidExt(string sExt)
{
bool flag=false;
string[] aExt = AllowExtSplit(|);
foreach(string filetype in aExt)
{
if(filetypeToLower()==sExt)
{
flag = true;
break;
}
}
return flag;
}

/// <summary>
/// 保存圖片
/// </summary>
/// <param name=image>Image 對象</param>
/// <param name=savePath>保存路徑</param>
/// <param name=ici>指定格式的編解碼參數</param>
void SaveImage(SystemDrawingImage imagestring savePathImageCodecInfo ici)
{
//設置 原圖片 對象的 EncoderParameters 對象
EncoderParameters parameters = new EncoderParameters();
parametersParam[] = new EncoderParameter(EncoderQuality ((long) ));
imageSave(savePath ici parameters);
parametersDispose();
}
#endregion

#region Methods

/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name=sourceImagePath>原圖片路徑(相對路徑)</param>
/// <param name=thumbnailImagePath>生成的縮略圖路徑如果為空則保存為原圖片路徑(相對路徑)</param>
/// <param name=thumbnailImageWidth>縮略圖的寬度(高度與按源圖片比例自動生成)</param>
public void ToThumbnailImages(string sourceImagePathstring thumbnailImagePathint thumbnailImageWidth)
{
string SourceImagePath = sourceImagePath;
string ThumbnailImagePath = thumbnailImagePath;
int ThumbnailImageWidth = thumbnailImageWidth;
string sExt = SourceImagePathSubstring(SourceImagePathLastIndexOf())ToLower();
if(SourceImagePathToString()==SystemStringEmpty) throw new NullReferenceException(SourceImagePath is null!);
if(!CheckValidExt(sExt))
{
throw new ArgumentException(原圖片文件格式不正確支持的格式有[ + AllowExt + ]SourceImagePath);
}
//從 原圖片 創建 Image 對象
SystemDrawingImage image = SystemDrawingImageFromFile(HttpContextCurrentServerMapPath(SourceImagePath));
int num = ((ThumbnailImageWidth / ) * );
int width = imageWidth;
int height = imageHeight;
//計算圖片的比例
if ((((double) width) / ((double) height)) >= f)
{
num = ((height * ThumbnailImageWidth) / width);
}
else
{
ThumbnailImageWidth = ((width * num) / height);
}
if ((ThumbnailImageWidth < ) || (num < ))
{
return;
}
//用指定的大小和格式初始化 Bitmap 類的新實例
Bitmap bitmap = new Bitmap(ThumbnailImageWidth num PixelFormatFormatbppArgb);
//從指定的 Image 對象創建新 Graphics 對象
Graphics graphics = GraphicsFromImage(bitmap);
//清除整個繪圖面並以透明背景色填充
graphicsClear(ColorTransparent);
//在指定位置並且按指定大小繪制 原圖片 對象
graphicsDrawImage(image new Rectangle( ThumbnailImageWidth num));
imageDispose();
try
{
//將此 原圖片 以指定格式並用指定的編解碼參數保存到指定文件
string savepath = (ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
SaveImage(bitmapHttpContextCurrentServerMapPath(savepath)GetCodecInfo((string)htmimes[sExt]));
}
catch(SystemException e)
{
throw e;
}
finally
{
bitmapDispose();
graphicsDispose();
}
}
#endregion

}
}

From:http://tw.wingwit.com/Article/program/net/201311/12672.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.