关闭

上传加水印

578人阅读 评论(0) 收藏 举报

//上传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">

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1371347次
    • 积分:19838
    • 等级:
    • 排名:第409名
    • 原创:448篇
    • 转载:1067篇
    • 译文:4篇
    • 评论:143条
    文章存档
    最新评论