您的位置:首页 > 其它

上传缩略图,等比例压缩

2010-08-16 15:08 113 查看
前些日子一位同学问我怎么上传缩略图,并可以指定图片的尺度而且进行等比压缩。晚上,自己没事在网上看到一些网友发的帖子,都是挺好,最后我也写了一个上传的方法。供大家参考:

代码

public byte[] UploadFile(FileUpload f_Ifile, int ThumbnailWidth, int ThumbnailHeight, string resPathId,string otherPath)
{
try
{
//获取客户端指定上传文件的访问
HttpPostedFile upfile = f_Ifile.PostedFile;
//得到上传文件的长度
int UpfileLength = upfile.ContentLength;
//没有选择上传的文件
if (UpfileLength == 0)
{
return null;
}
//得到上传文件的客户端MIME类型
string contentType = upfile.ContentType;
byte[] fileArray = new Byte[UpfileLength];
Stream fileStream = upfile.InputStream;
fileStream.Read(fileArray, 0, UpfileLength);
string filename = Path.GetFileName(upfile.FileName);
string sExt = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".")).ToLower();
//读取xml判断上传的文件格式及大小
string xmlPath = System.Web.HttpContext.Current.Server.MapPath("~/File.xml");
bool postfix = Tools.UpDownFile.VerdictPostfixByFile(filename, xmlPath, "FileManager", "id", "UpImgFile", "format");
bool size = Tools.UpDownFile.VerdictSizeByFile(f_Ifile, xmlPath, "FileManager", "id", "ImageSize", "size");
System.Drawing.Image xImage;
//缩略图的高,宽
int PhotoHeight, PhotoWidth;
//创建新的图形
Rectangle NewPhoto;
//位图图像
System.Drawing.Bitmap xBitmap;
//路径
//string filePath = "";
if (postfix == true && size == true)
{
xImage = System.Drawing.Bitmap.FromStream(upfile.InputStream);
//赋值,高和宽
//this.ThumbnailHeight = xImage.Height.ToString();
//this.ThumbnailWidth = xImage.Width.ToString();
if (this.txtwidth.Text == "")
{
ThumbnailWidth = Convert.ToInt32(xImage.Width.ToString());
}
else
{
ThumbnailWidth = Convert.ToInt32(this.txtwidth.Text);
}
if (this.txtheight.Text == "")
{
ThumbnailHeight = Convert.ToInt32(xImage.Height.ToString());
}
else
{
ThumbnailHeight = Convert.ToInt32(this.txtheight.Text);
}
//上传资源模块的缩略图
if (resPathId != "" && otherPath=="")
{
ResPath rp = BLL.ResPathManager.GetResPathByResModelId(Convert.ToInt32(resPathId));
if (rp.ResPaths != "")
{
filepath = rp.ResPaths + @"\Pic";
}
else
{
return null;
}
}
//上传的不是资源文件的缩略图
else if (resPathId == "" && otherPath != "")
{
filepath = otherPath;
if (!Tools.IOHelp.CreateDirectory(filepath))
{
return null;
}
}
else
{
return null;
}
string  ranNum= Tools.FormatStr.GetUpFileName() + sExt;
filepath = filepath + @"\" + ranNum;
xBitmap = new System.Drawing.Bitmap(fileStream);

PhotoHeight = xBitmap.Height;
PhotoWidth = xBitmap.Width;
if (Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) > Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth))
{
PhotoHeight = Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight) / Convert.ToDecimal(ThumbnailWidth)) * Convert.ToDecimal(PhotoWidth));
NewPhoto = new Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width) - Convert.ToDecimal(PhotoWidth)) / 2), 0, PhotoWidth, PhotoHeight);
}
else if (Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) < Convert.ToDecimal(ThumbnailHeight) / Convert.ToDecimal(ThumbnailWidth))
{
PhotoWidth = Convert.ToInt16((Convert.ToDecimal(ThumbnailWidth) / Convert.ToDecimal(ThumbnailHeight)) * Convert.ToDecimal(PhotoHeight));

NewPhoto = new
Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width) - Convert.ToDecimal(PhotoWidth)) / 2), 0, PhotoWidth, PhotoHeight);
}
else
{
NewPhoto = new Rectangle(0, 0, PhotoWidth, PhotoHeight);
}

System.Drawing.Image myBitmap;
myBitmap = xBitmap.Clone(NewPhoto, System.Drawing.Imaging.PixelFormat.DontCare);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image myThumbnail =  xImage.GetThumbnailImage(ThumbnailWidth, ThumbnailHeight, myCallback, IntPtr.Zero);

myThumbnail.Save(filepath);
this.Label1.Text = ranNum;

myThumbnail.Dispose();
myBitmap.Dispose();
xBitmap.Dispose();
return fileArray;
}
}
catch (Exception   ex)
{

return null;
}
return null;
}


上述为上传缩略图的方法,涉及到读取xml文件和存储地址,不是很完整,掉了个方法,补上:

private static bool ThumbnailCallback() { return false; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: