您的位置:首页 > 其它

创建缩略图的几种方法(都是从网上搜罗的,只是使用后发表自己的评价)

2008-11-05 09:42 671 查看
using System;
using System.Drawing;

namespace ThumbnailDemo
{
/// <summary>
/// Thumbnail 的摘要说明。
/// </summary>
public class Thumbnail
{
public Thumbnail()
{}

//************************************************************//
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
#region 我的推荐
#region 自定义缩略图的宽度和高度
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{

System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);

}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
#endregion

#region 按比例缩小图片,自动计算高度
/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
public void SmallPicByWidth(string strOldPic, string strNewPic, int intWidth)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight=(intWidth *objPic.Height ) / objPic.Width ;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic);
}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
#endregion

#region 按比例缩小图片,自动计算宽度
/// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPicByHeight(string strOldPic, string strNewPic, int intHeight)
{
System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
//根据高度计算出缩略图的宽度
int intWidth = (intHeight * objPic.Width) / objPic.Height;//注意,这里进行了隐式转换!
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);

}
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
#endregion
#endregion

#region 高精度(作者说效果很好,失真度很小,但我在使用的过程中发现缩略图的效果一般般)
public void MakeSmallImg(System.Web.HttpPostedFile postFile,string saveImg,System.Double Width,System.Double Height)
{

//原始图片名称
string originalFilename = postFile.FileName;
//生成的高质量图片名称
string strGoodFile = saveImg;

//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream,true);

System.Double NewWidth,NewHeight;
if(image.Width>image.Height)
{
NewWidth=Width;
NewHeight=image.Height*(NewWidth/image.Width);
}
else
{
NewHeight=Height;
NewWidth=(NewHeight/image.Height)*image.Width;
}

if (NewWidth>Width)
{
NewWidth=Width;
}
if (NewHeight>Height)
{
NewHeight=Height;
}

//取得图片大小
System.Drawing.Size size = new Size((int)NewWidth,(int)NewHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);

///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();

///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();

//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);

g.Dispose();
image.Dispose();
bitmap.Dispose();
}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐