/// <summary>
/// asp.net上傳圖片并生成縮略圖
/// </summary>
/// <param name="upImage">HtmlInputFile控件</param>
/// <param name="sSavePath">保存的路徑,些為相對服務(wù)器路徑的下的文件夾</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 "沒有選擇圖片";
}
}