您的位置:首页 > 其它

上传图片,限制类型,大小,规格

2012-02-27 00:03 288 查看
/// <summary>
/// asp.net上传图片并生成缩略图
/// </summary>
/// <param name="upImage">HtmlInputFile控件</param>
/// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>
/// <param name="sThumbExtension">缩略图的thumb</param>
/// <param name="intThumbWidth">生成缩略图的宽度</param>
/// <param name="intThumbHeight">生成缩略图的高度</param>
/// <returns>缩略图名称</returns>
public string UpLoadImage(FileUpload upImage, string savePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
if (upImage.HasFile)
{
if (upImage.PostedFile.ContentLength > 0 && upImage.PostedFile.ContentLength / 1024 < 1024)
{
string imgType = upImage.PostedFile.ContentType.ToString(); // 图片类型
if (imgType.Equals("image/pjpeg") || imgType.Equals("image/gif") || imgType.Equals("image/png"))
{
//取得上传文件的扩展名
string strExt = upImage.PostedFile.FileName.Substring(upImage.PostedFile.FileName.LastIndexOf("."));
// 自定义名称保存
string newImaName = (sThumbExtension + strExt).ToString();
// 保存到指定目录
string newsavePath = Server.MapPath(savePath);

System.Drawing.Image img = System.Drawing.Image.FromFile(upImage.PostedFile.FileName);
if (img.Width < intThumbWidth && img.Height < intThumbHeight)
{
return "图片的宽度和高度不符合(" + intThumbWidth + "*" + intThumbHeight+")";
}
else
{
upImage.PostedFile.SaveAs(newsavePath + newImaName);
return "上传图片成功.";
}

//try
//{
// //原图加载
// using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(upImage.PostedFile.FileName))
// {
// //原图宽度和高度
// int width = sourceImage.Width;
// int height = sourceImage.Height;
// int smallWidth;
// int smallHeight;
// //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
// if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
// {
// smallWidth = intThumbWidth;
// smallHeight = intThumbWidth * height / width;
// }
// else
// {
// smallWidth = intThumbHeight * width / height;
// smallHeight = intThumbHeight;
// }

// // 新建一个图板,以最小等比例压缩大小绘制原图
// using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
// {
// //绘制中间图
// using (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.Black);
// g.DrawImage(
// sourceImage,
// new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
// new System.Drawing.Rectangle(0, 0, width, height),
// System.Drawing.GraphicsUnit.Pixel);
// }
// //新建一个图板,以缩略图大小绘制中间图
// using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
// {
// //绘制缩略图
// using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
// {
// //高清,平滑
// g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// g.Clear(Color.Black);
// int lwidth = (smallWidth - intThumbWidth) / 2;
// int bheight = (smallHeight - intThumbHeight) / 2;
// g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
// g.Dispose();
// //缩略图保存的绝对路径
// bitmap1.Save(newsavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
// }
// }
// }

// }
//}
//catch(Exception ex)
//{
// return ex.Message.ToString();
//}

}
else
{
return "图片类型不正确.";
}

}
else
{
return "请选择不大于1M的图片.";
}
}
else
{
return "没有选择图片";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: