#region 上传文件
/// <summary>
/// 文件上传
/// </summary>
/// <returns>文件上传到服务器的路径</returns>
void upfile()
{
string strWidth = "";
Random myrd = new Random();
string path, filename; //上传文件路径,上传文件名称
path = Server.MapPath("../../upfile/old/");
filename = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "").Replace(":", "").Replace(" ", "") + myrd.Next(1000).ToString(); //上传文件名称
//取得文件的扩展名
string fileExtension = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower();
path = path + filename + fileExtension;
if (fileExtension != ".jpg" && fileExtension != ".gif" && fileExtension != ".jpeg"&&fileExtension!=".png")
{
JScript.Alert("错误!!文件类型必须为jpg或者gif!");
}
else
{
File1.PostedFile.SaveAs(path);
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
int w = image.Width;
strWidth = (w >= 600) ? " width=/"600/" " : "";
int h = image.Height;
g.DrawImage(image, 0, 0, w, h);
w = GetFontSize(w);
System.Drawing.Font f = new System.Drawing.Font("verdana", w);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.White);
string addtext = "图片来自:http://www.Newboy2010.gicp.com(永远在一起)";
h = h - GetFontHeight(h);
g.DrawString(addtext, f, b, 0, h);
g.Dispose();
//保存加水印过后的图片
string newpath = Server.MapPath("../../upfile/users/") + filename + fileExtension;
image.Save(newpath);
image.Dispose();
string strFileName = "upfile/users/" + filename + fileExtension;
string js = @"<script language=javascript>";
js += " window.returnValue = '<img src= /"" + strFileName + "/" " + strWidth + "/>';";
js += "window.close();";
js += "</script>";
Response.Write(js);
}
}
public bool ThumbnailCallback()
{
return false;
}
private int GetFontHeight(int intHeight)
{
if (intHeight <= 100)
return 10;
if (intHeight > 100 && intHeight <= 200)
return 15;
else if (intHeight > 200 && intHeight <= 300)
return 20;
else if (intHeight > 300 && intHeight <= 400)
return 25;
else if (intHeight > 400 && intHeight <= 500)
return 30;
else if (intHeight > 500 && intHeight <= 600)
return 35;
else if (intHeight > 600 && intHeight <= 800)
return 40;
else
return 50;
}
private int GetFontSize(int intWidth)
{
if (intWidth <= 100)
return 4;
else if (intWidth > 100 && intWidth <= 200)
return 6;
else if (intWidth > 200 && intWidth <= 300)
return 8;
else if (intWidth > 300 && intWidth <= 400)
return 10;
else if (intWidth > 400 && intWidth <= 500)
return 12;
else if (intWidth > 500 && intWidth <= 600)
return 14;
else if (intWidth > 600 && intWidth <= 800)
return 16;
else
return 20;
}
#endregion
/