/// <summary>
/// 產生縮圖
/// </summary>
/// <param name="fileName">原檔案</param>
/// <param name="width">寬度</param>
/// <param name="height">高度</param>
private void CreateThumbnailImage(string fileName, string smallPath, int width, int height)
{
//著作權資訊
string strAdd = "www.wander.com";
//產生的縮圖的名字=年月日+檔案大小(防止檔案名稱相同)
string newFileName = smallPath;
System.Drawing.Image image, newImage;
//載入原映像
image = System.Drawing.Image.FromFile(Server.MapPath(fileName));
//回調
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(callBack);
//產生縮圖
newImage = image.GetThumbnailImage(width, height, callb, new System.IntPtr());
AddInfo(newImage, strAdd, newFileName, 16);
image.Dispose();
newImage.Dispose();
Image1.ImageUrl = newFileName;
}
/// <summary>
/// 添加著作權資訊
/// </summary>
/// <param name="image"></param>
/// <param name="strAdd"></param>
/// <param name="newFileName"></param>
/// <param name="fontSize"></param>
private void AddInfo(System.Drawing.Image image, string strAdd, string newFileName, int fontSize)
{
Response.Clear();
Bitmap b = new Bitmap(image);
Graphics g = Graphics.FromImage(b);
g.DrawString(strAdd, new Font("宋體", fontSize), new SolidBrush(Color.Red), image.Width / 2 - 80, image.Height - 30);
b.Save(Server.MapPath(newFileName), System.Drawing.Imaging.ImageFormat.Gif);
}
private bool callBack()
{
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (loadFile.PostedFile != null)
{
//判斷是不是影像檔
if (loadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
//檔案類型錯誤
}
else
{
string ext = loadFile.PostedFile.FileName.Substring(loadFile.PostedFile.FileName.LastIndexOf("."));
//產生新的原檔案名稱 年月日+檔案大小+副檔名
string path = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + ext;
//加上著作權資訊的檔案
string newPath = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "new" + ext;
//縮圖檔案名稱
string newFileName = "uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "small.gif";
//上傳原圖
loadFile.PostedFile.SaveAs(Server.MapPath(path));
//產生縮圖
CreateThumbnailImage(path, newFileName, 200, 300);
//給原圖加上著作權資訊
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(path));
AddInfo(image, "www.wander.com", newPath, 20);
image.Dispose();
}
}
}