这种FTP上传形式是把客户端的文件原文不动的传了上去,如果用户手上只有数码图片呢?而数码图片默认一般都在2M以上,一般的用户他们都不会对图片进行优化处理,如果是上传原图片的话,这样起码有两个问题:
第一:在上传速度上成问题;
第二:在网页加载的时候也会太慢.所以我们想将原图片进行缩放处理后只上传处理后的小图片而不用上传原始图片.
程序如下:

Code

/**//// <summary>
/// 上传图片,生成小图保存,并不保存原图片,并保存到特定的目录中
/// by minjiang 08-6-4
/// </summary>
/// <param name="Fileup">上传图片控件</param>
/// <param name="sSavePath">保存图片文件夹</param>
/// <param name="iMaxSize">上传图片大小上限,..k</param>
/// <returns></returns>
public string[] upSmallPicture(ref System.Web.UI.HtmlControls.HtmlInputFile Fileup, string sSavePath, string preFileName, int iWidth, int iHeight)

{
HttpFileCollection files = HttpContext.Current.Request.Files;
string strfileoldpath; //上传原图片的保存路径
string strfileoldname;//图片保存后名称
string fileName, fileExtension;

string[] arrayImageUrl = new string[4]
{ "", "", "", "" };
try

{
//没有文件上传
if (Fileup.PostedFile.FileName == "")
return arrayImageUrl;
HttpPostedFile postedFile = null;
if (files.Count > 0)

{
postedFile = files[0];

}
else

{
arrayImageUrl[2] = "errorNoFile";
return arrayImageUrl;

}
Stream _fileStram = (Stream)postedFile.InputStream;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == "")
return arrayImageUrl;
fileExtension = System.IO.Path.GetExtension(fileName);
//判断上传文件类型
string filetype = Fileup.PostedFile.ContentType;
//判断是否图片
if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif"))

{
arrayImageUrl[2] = "errorT";
return arrayImageUrl;
}


//检查保存文件的目录是否存在,否则创建
strfileoldpath = CheckProductPictureML(sSavePath);

//为文件命名,然后保存
Random ra = new Random();

strfileoldname = preFileName + "_" + ra.Next(100000, 999999).ToString() + fileExtension;
strfileoldpath += "\\" + strfileoldname;

System.Drawing.Image img = System.Drawing.Image.FromStream(_fileStram);
//计算缩放后图片的真正尺寸
int iWidthTemp = iWidth;
int iHeightTemp = iHeight;
double dHeight = (double)iHeight;
double dWidth = (double)iWidth;
//图片实际的高
double dImgHeight = (double)img.Height;
//图片实际的宽
double dImgWidth = (double)img.Width;
//如果上传的图片的宽大于设置的宽或者图片的高大于设置的高
if (dImgWidth > dWidth || dImgHeight > dHeight)

{
if (dImgWidth / dWidth > (dImgHeight) / dHeight)

{

dHeight = dWidth / dImgWidth ;
dHeight = dHeight * dImgHeight ;
iHeight = Convert.ToInt32(dHeight);
}
else

{

dWidth = dHeight / dImgHeight;
dWidth = dWidth * dImgWidth ;
iWidth = Convert.ToInt32(dWidth);
}
if (iWidth > iWidthTemp)

{
iWidth = iWidthTemp;
}
if (iHeight > iHeightTemp)

{
iHeight = iHeightTemp;

}


}
else

{

iWidth = (int)dImgWidth ;
iHeight = (int)dImgHeight ;

}

//当上传的小图片的长和高有一个为0时,将它设置成1 解决不能上传的问题
if (iWidth < 1)

{ iWidth = 1; }
if (iHeight < 1)

{ iHeight = 1; }
System.Drawing.Bitmap myBitmap = new Bitmap(img ,iWidth ,iHeight );
//注释部分为Ftp上传
//Stream s = null;
//myBitmap.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
//保存原始图像
//调用方法 mFtpUpload(Stream _fileStream, string strfileoldname, string ftpUserID, string ftpPassword)
//保存原始图像
switch (fileExtension)

{
case ".gif":
myBitmap.Save(strfileoldpath, System.Drawing.Imaging.ImageFormat.Gif);
break;
default:
myBitmap.Save(strfileoldpath, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
//释放资源
this.clearStram(_fileStram);
this.clearBitmap(myBitmap );
this.clearImg(img);

return arrayImageUrl;

}
catch

{
arrayImageUrl[2] = "error";
return arrayImageUrl;
}
}

/**//// <summary>
/// 释放文件流
/// </summary>
/// <param name="_fileStram">文件流</param>
private void clearStram(Stream _fileStram)

{
if (_fileStram != null)

{
_fileStram.Close();
_fileStram.Dispose();
_fileStram.Flush();
}
}

/**//// <summary>
/// 释放位图
/// </summary>
/// <param name="_bitmap">位图</param>
private void clearBitmap(Bitmap _bitmap)

{
if (_bitmap != null)

{
_bitmap.Dispose();

}

}

/**//// <summary>
/// 释放图对象
/// </summary>
/// <param name="_bitmap">图</param>
private void clearImg(System.Drawing.Image img)

{
if (img != null)

{
img.Dispose();

}

}
主要的原理其实特别简单,就是通过上传组件来得到一个客户端文件流,然后通过这个文件流得到一个Image对象,System.Drawing.Image img = System.Drawing.Image.FromStream(_fileStram);
Bitmap 构造函数 (Image, Int32, Int32) :
从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。
参数 original
类型:System.Drawing.Image
从中创建新 Bitmap 的 Image。
width
类型:System..::.Int32
新 Bitmap 的宽度(以像素为单位)。
height
类型:System..::.Int32
新 Bitmap 的高度(以像素为单位)。
通过Bitmap类的构造函数,可以等到一个经过缩放后的位图对象,最后通过位图对象来将文件进行保存.本人对Image..::.GetThumbnailImage这个方法进行了实验,发现这种方式生成的小图会出现失真的情况.
注:
以上观点纯属本人意见,如有缺陷请大家指教.
本人对WEB程序优化特别感兴趣,望同样对这方面有兴趣的朋友多多发表些意见及经验.