首页 > 编程 > .NET > 正文

用C#轻松地在DOTNET中实现缩略图

2024-07-21 02:19:35
字体:
来源:转载
供稿:网友
以前,在页面上实现缩略图必须借助第三方组件。现在,有了.net,就可以很轻松地实现缩略图。下面就是实现缩略图的例子。

关键字:c# ,asp.net,缩略图

实例下载:http://www.lionsky.net/mywebsite/downsoft/list.aspx?id=221

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

}
}
中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表