c#图片上传和加水印
添加一个FileUpload控件,然后再添加一个image控件
//上传图片
protected void BtnUpload_Click(object sender, EventArgs e)
{
//上传图片
//取出文件路径
string FileFullName = this.FileUpload1.PostedFile.FileName;
//取出文件名
string FileName = FileFullName.Substring(FileFullName.LastIndexOf("\\")+1);
//取出文件类型
string FileType = FileFullName.Substring(FileFullName.LastIndexOf(".") + 1);
//获取文件要存的路径
string SavePath = Server.MapPath("upload") + "\\" + FileName;
FileType = FileType.ToLower();
if (FileType == "jpg" || FileType == "bmp" || FileType == "gif")
{
//保存路径
this.FileUpload1.PostedFile.SaveAs(SavePath);
this.Image1.ImageUrl = SavePath;
}
else
{
Response.Write("<script language='javascript'>alert('对不起,图片格式错误');</script>");
}
}
/**/
/**/
/**/
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
protected void AddShuiYinWord(string Path, string Path_sy)
{
string addText = "测试水印";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
g.DrawString(addText, f, b, 15, 15);
g.Dispose();
image.Save(Path_sy);
image.Dispose();
}
/**/
/**/
/**/
/// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage,new System.Drawing.Rectangle(0,0, 100, 20), 0, 0, copyImage .Width,copyImage .Height, System.Drawing.GraphicsUnit.Pixel);
//g.DrawImage(水印图象名,水印区域大小(x坐标,y坐标,宽,高),水印图象x坐标,水印图象y坐标,水印图象宽,水印图象高,方式)
g.Dispose();
image.Save(Path_syp);
image.Dispose();
}