独立购物网系统研发讨论

携购独立网店系统,ShopXG免费独立网店系统
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

自动生成任意规格的缩略图

Posted on 2009-01-12 15:12  吴康彬  阅读(385)  评论(0编辑  收藏  举报
      做过网站的朋友想必都知道缩略图的作用,特别是独立购物网系统中,一般一个页面上需要展示出几十个产品供客户查看,如果都用大图显示,那么一个页面全部打开需要很长的时间。而如果把大图自动生成缩略图,那么打开的速度可以快很多。
    图片缩放的程序很简单。但是非常的实用,这里我提供给大家一个可以自动生成任意规格的缩略图代码,你可以把这段代码加入到你的程序里,那么甚至可以让客户自定义,想看多大的缩略图都可以。
    以下函数运行生成的效果图如下:
VTEditor网站模块可视化编辑系统截图
图1:原始图片
图2:使用程序缩放比例:400*200后的图片
图3:使用程序缩放比例:200*300后的图片
		public static bool SmallPic(string strOldPic, string strNewPic, int widthEx, int heightEx)
{//提供原始图片,目标路径,以及想要生成的缩略图尺寸
System.Drawing.Bitmap objPic,objNewPic;
objPic = new System.Drawing.Bitmap(strOldPic);
int width,height;
width = objPic.Width;
height = objPic.Height;
if((int)(height/heightEx) >= (int)(width/widthEx))
{//高度压缩比例大于宽度
SmallPicW(strOldPic,strNewPic,heightEx,widthEx);
objPic.Dispose();
return true;
}
else
{//高度压缩比例小于宽度
SmallPicH(strOldPic,strNewPic,widthEx,heightEx);
objPic.Dispose();
return true;
}
}

    ///下面这个函数处理高度压缩比例小于宽度的情况
		private static void SmallPicH(string strOldPic, string strNewPic, int intWidth, int descHeight)
{
System.Drawing.Bitmap objPic,smallObjPic,objNewPic;
objPic = null;
objNewPic = null;
smallObjPic = null;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
//将原始图片放入到内存里
int intHeight=(intWidth * objPic.Height )/ objPic.Width;
smallObjPic = new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic = new System.Drawing.Bitmap(intWidth, descHeight, PixelFormat.Format64bppPArgb);
//在内存里生成一张跟缩略图尺寸一样的空图片。
objNewPic.SetResolution(objPic.HorizontalResolution, objPic.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(objNewPic);
if (objPic.Width < intWidth)
{
grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, intWidth, descHeight);
grPhoto.DrawImage(smallObjPic, new Rectangle((intWidth - objPic.Width) / 2,
(descHeight - objPic.Height) / 2, objPic.Width, objPic.Height));
}
else
{
grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, intWidth, descHeight);
grPhoto.DrawImage(smallObjPic, new Rectangle(0, (descHeight - intHeight) / 2, intWidth, intHeight)
, 0, 0, intWidth, intHeight, System.Drawing.GraphicsUnit.Pixel);
}
#region  保存图片
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 90;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含对应的图片编解码程序
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//
break;
}
}
if (jpegICI != null)
{
objNewPic.Save(strNewPic, jpegICI, encoderParams);
}
else
{
objNewPic.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Jpeg);
}
objPic.Dispose();
objPic=null;
objNewPic.Dispose();
objNewPic=null;
smallObjPic.Dispose();
smallObjPic = null;
#endregion
}
catch(Exception exp){
}
finally
{
if(objPic != null)
objPic.Dispose();
if(objNewPic != null)
objNewPic.Dispose();
if(smallObjPic != null)
smallObjPic.Dispose();
objPic=null;
objNewPic=null;
smallObjPic = null;
}
}

    ///以下函数是处理高度压缩比例大于宽度的情况
		private static void SmallPicW(string strOldPic, string strNewPic, int intHeight , int descWidth)
{
System.Drawing.Bitmap objPic,smallObjPic,objNewPic;
objPic = null;
objNewPic = null;
smallObjPic = null;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth=(intHeight * objPic.Width) / objPic.Height;
smallObjPic = new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic = new System.Drawing.Bitmap(descWidth, intHeight, PixelFormat.Format64bppPArgb);
objNewPic.SetResolution(objPic.HorizontalResolution, objPic.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(objNewPic);
if (objPic.Height < intHeight)
{//
grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, descWidth, intHeight);
Rectangle r = new Rectangle((descWidth - objPic.Width) / 2, (intHeight - objPic.Height) / 2,
intWidth, objPic.Height);
grPhoto.DrawImage(smallObjPic, r);
}
else
{
grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, descWidth, intHeight);
grPhoto.DrawImage(smallObjPic, new Rectangle((descWidth - intWidth) / 2, 0, intWidth, intHeight),
0, 0, intWidth, intHeight, System.Drawing.GraphicsUnit.Pixel);
}
#region
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 90;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//
break;
}
}
if (jpegICI != null)
{
objNewPic.Save(strNewPic, jpegICI, encoderParams);
}
else
{
objNewPic.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Jpeg);
}
objPic.Dispose();
objPic = null;
objNewPic.Dispose();
objNewPic = null;
smallObjPic.Dispose();
smallObjPic = null;
#endregion
}
catch(Exception exp)
{
throw exp;
}
finally
{
if(objPic != null)
objPic.Dispose();
if(objNewPic != null)
objNewPic.Dispose();
if(smallObjPic != null)
smallObjPic.Dispose();
objPic=null;
smallObjPic = null;
objNewPic=null;
}
}

        欢迎大家与我交流。QQ:8814730 Email:wkb@xiegoo.com