/// <summary>
/// 文件上传,适用于.net2.0
/// </summary>
/// <param name="file">上传域FileUpload控件</param>
/// <param name="SavePath">图片存储路径,默认为upload/</param>
/// <param name="WatermarkImgFile">水印图片地址,留空则无水印</param>
/// <returns>返回文件名</returns>
public string FileUp(System.Web.UI.WebControls.FileUpload file,string SavePath, string WatermarkImgFile)
{
if (file.HasFile)
{
string picName = file.PostedFile.FileName;
int pos = picName.LastIndexOf(".");
string excName = picName.Substring(pos);
kin.Web.CommTool commtool = new kin.Web.CommTool();
string newName = kin.Web.CommTool.RandomFileName() + excName;
string phyicPath = HttpContext.Current.Server.MapPath(kin.Web.CommTool.GetAppPath + SavePath);
//如果目录不存在,则创建
if (Directory.Exists(phyicPath) == false)
Directory.CreateDirectory(phyicPath);
file.PostedFile.SaveAs(phyicPath + newName);
if (WatermarkImgFile == "")
{
//无水印,直接返回
return newName;
}
System.Drawing.Image image = System.Drawing.Image.FromFile(phyicPath + newName);
//if (WatermarkType == 1)
//{
////加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
//Graphics g = Graphics.FromImage(image);
//g.DrawImage(image, 0, 0, image.Width, image.Height);
//Font f = new Font("Verdana", 12);
//Brush b = new SolidBrush(Color.White);
//string addText = WatermarkString;
//g.DrawString(addText, f, b, 10, 10);
//g.Dispose();
//}
//else if (WatermarkType == 2) {
//加图片水印
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(kin.Web.CommTool.GetAppPath + WatermarkImgFile));
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//}
//保存加水印过后的图片,删除原始图片
string newName2 = kin.Web.CommTool.RandomFileName() + excName;
image.Save(phyicPath + newName2);
image.Dispose();
File.Delete(phyicPath + newName);
return newName2;
}
else
{
return "";
}
}