/// <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; } } /// <summary> /// 按比例缩小图片,自动计算高度 /// </summary> /// <param name="strOldPic">源图文件名(包括路径)</param> /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param> /// <param name="intWidth">缩小至宽度</param> public void SmallPic(string strOldPic, string strNewPic, int intWidth) { System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intHeight=(intWidth / 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; } } /// <summary> /// 按比例缩小图片,自动计算宽度 /// </summary> /// <param name="strOldPic">源图文件名(包括路径)</param> /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param> /// <param name="intHeight">缩小至高度</param> public void SmallPic(string strOldPic, string strNewPic, int intHeight) { System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intWidth=(intHeight / 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; } }
使用javascript实现相同的功能。
function resizeImage(image,width,height) { var w = image.width; var h = image.height; if(w>width){ image.width=width; image.height = parseInt(h*(width/w)); } }
这几个函数主要是想用来进行图片大小的控制,因为在blog中,图片有时候过大会破坏整个页面的观感,所有准备对图片进行按比例对缩放。初步想法是在写入数据的时候通过对 <img> 的 onload 属性的写入来进行控制。
评论回复