/// <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();
}
}
}