protected void bt_upload_Click(object sender, EventArgs e)
{
//檢查上傳文件的格式是否有效
if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") 0)
{
Response.Write("上傳圖片格式無效!");
return;
}
//生成原圖
Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原圖寬度
int oHeight = oImage.Height; //原圖高度
int tWidth = 100; //設(shè)置縮略圖初始寬度
int tHeight = 100; //設(shè)置縮略圖初始高度
//按比例計算出縮略圖的寬度和高度
if (oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//生成縮略原圖
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設(shè)置高質(zhì)量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.Clear(Color.Transparent); //清空畫布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string oFullName = Server.MapPath(".") + "/image/" + "o" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原圖的物理路徑
string tFullName = Server.MapPath(".") + "/image/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存縮略圖的物理路徑
try
{
//以JPG格式保存圖片
oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//釋放資源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
}
#region 上傳圖片 并生成縮略圖
/// summary>
/// 上傳圖片生成縮略圖
/// /summary>
/// param name="originalImagePath">圖片源路徑/param>
/// param name="thumbnailPath">縮略圖路徑(物理路徑)/param>
/// param name="width">縮略圖寬度/param>
/// param name="height">縮略圖高度/param>
/// param name="mode">生成縮略圖的方式/param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) {
//從路徑中獲取源圖片的文件
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
//獲取圖片的寬度
int ow = image.Width;
//獲取圖片的高度
int oh = image.Height;
//生成縮略圖的方式
switch (mode) {
case "HW":
break;
case "W"://指定寬度 高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定圖片的高度 寬按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://如果為裁減模式 則不變形
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
//縮略圖片的寬度
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else {
ow = originalImage.Width;
//縮略圖片的高度
oh = originalImage.Width * toheight / towidth;
x = 0;
y(originalImage.Height - oh) / 2;
}
break;
default: break;
}
//新建一個bmp圖片
Bitmap bitmap = new Bitmap(towidth, toheight);
//新建一個畫布 以BitMap 寬 高作為畫布的大小
Graphics g = Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = InterpolationMode.High;
//以高質(zhì)量 低速度 呈現(xiàn)
g.SmoothingMode = SmoothingMode.HighQuality;
//清空畫布 以白色背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage,new Rectangle(towidth,toheight),new Rectangle(x,y,ow,oh),GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally {
//釋放資源
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion