//上传click 方法如下:
private void Button1_Click(object sender, System.EventArgs e)
{
string clientFullName = File1.PostedFile.FileName.ToString();
string extendName = clientFullName.Substring(clientFullName.LastIndexOf(".")+1).Trim().ToLower();
if(extendName == "jpg" || extendName == "gif")
{
string dateName = DateTime.Now.ToString("yyMMddhhmmss");
string dateFullName = Server.MapPath("test") + "//"+dateName+ "." + extendName;
this.File1.PostedFile.SaveAs(dateFullName);
System.Drawing.Image image = System.Drawing.Image.FromFile(dateFullName);
Graphics myGraphic = Graphics.FromImage(image);
Color myColor = Color.FromArgb(128,255,0,0);
SolidBrush myBrush = new SolidBrush(myColor);
Font myFont = new Font("Arial",16);
//Font myFont = new Font("Arial",image.Width/10); //大小根据图片决定字体大小
string myText = "HelloWorld";
// int imgX = image.Width/2 - (image.Width/10 * myText.Length)/2 ;
// int imgY = image.Height/2-(myFontHeight)/2; //决定位置
int imgX = 5;
int imgY = 5;
myGraphic.DrawString(myText, myFont, myBrush, imgX, imgY);
string waterMarkPath =Server.MapPath("test")+"//"+dateName+ "_wm." + extendName;
//image.Save(waterMarkPath);
//this.ibImage.ImageUrl = waterMarkPath;
//释放资源,以方便Del没加waterMark的图片
myGraphic.Dispose();
image.Dispose();
if(System.IO.File.Exists(dateFullName))
{
System.IO.File.Delete(dateFullName);
}
Response.Write("上传成功!");
}
else
{
Response.Write("格式不对");
}
}
client端加上效果会更好些
<INPUT id="File1" style="Z-INDEX: 103; LEFT: 120px; POSITION: absolute; TOP: 96px" type="file"
name="File1" runat="server" onpropertychange="document.all.ibImage.src='file:///'+this.value">