這二天想了想決定把自己先前做的項目中的小的功能,整理一下,奉獻大家!經大家提議,我決定加上原始碼,但是原始碼不一定都是我寫的,有的來自書上,有的來自網上,有的來自開源項目,與我自己的代碼有不相符的,那就是先前的例子!
原始碼:smallImg.rar
以下是上傳一張800*1000的,分別產生不同縮圖
命名空間:
using System.IO;
核心代碼:
System.Drawing.Image.GetThumbnailImageAbort callb = null;
try
{
// 儲存到指定的檔案夾
File1.PostedFile.SaveAs(Server.MapPath("/semir/ImgSides/") + sidePath);
MyImage = System.Drawing.Image.FromFile(Server.MapPath("/semir/ImgSides/") + sidePath);
// 儲存大圖(原圖)
NewImage = MyImage.GetThumbnailImage(800, 1000, callb, new System.IntPtr());
NewImage.Save(Server.MapPath("/semir/ImgSides/") + "big" + sidePath);
// 儲存中圖
NewImage = MyImage.GetThumbnailImage(400, 500, callb, new System.IntPtr());
NewImage.Save(Server.MapPath("/semir/ImgSides/") + "middle" + sidePath);
// 單款衣服的圖片大小
NewImage = MyImage.GetThumbnailImage(255, 319, callb, new System.IntPtr());
NewImage.Save(Server.MapPath("/semir/ImgSides/") + "SingleImage" + sidePath);
// 儲存小圖
NewImage = MyImage.GetThumbnailImage(115, 144, callb, new System.IntPtr());
NewImage.Save(Server.MapPath("/semir/ImgSides/") + "small" + sidePath);
// 儲存極小圖
NewImage = MyImage.GetThumbnailImage(45, 56, callb, new System.IntPtr());
NewImage.Save(Server.MapPath("/semir/ImgSides/") + "dinky" + sidePath);
MyImage.Dispose();
NewImage.Dispose();
// 一定要釋放,否則進程被佔用
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}