c#图片添加水印
今天讲一个上传图片添加水印的方法,直接上代码吧
protected void Button1_Click(object sender, EventArgs e) { int location = Convert.ToInt32(this.DropDownList1.SelectedValue);//获取水印放置位置 //判断FileUpload里是否有文件地址 if (FileUpload1.HasFile) { if ((FileUpload1.PostedFile.ContentType == "image/pjpeg") || (FileUpload1.PostedFile.ContentType == "image/jpeg") || (FileUpload1.PostedFile.ContentType == "image/gif") || (FileUpload1.PostedFile.ContentType == "image/bmp") || (FileUpload1.PostedFile.ContentType == "image/x-png") || (FileUpload1.PostedFile.ContentType == "image/png"))//获取客户端发送的文件的MIME内容类型 { //上传文件总大小 int fileLength = 0; fileLength = fileLength + FileUpload1.PostedFile.ContentLength; //大小不能超过maxLengthk int maxLength = 2048; int sysLength = maxLength * 1024; if (fileLength > sysLength) { Response.Write("<script>alert(''''该图片大小超过2M限制'''')</script>"); } else { string[] strSpil = FileUpload1.FileName.Split(''''.'''');//将此地址用.号进行分割(img/1.jpg) string strEnd = strSpil[strSpil.Length - 1].ToLower();//得到后面的("jpg", "gif", "bmp", "png","jpeg","JPG","GIF","BMP","PNG","JPEG") string[] strPic = new string[] { "jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF", "BMP", "PNG", "JPEG" };//定义一个数组里面放文件格式 List<string> arry = new List<string>(); arry.AddRange(strPic); //定义一个可变数组,用于放文件格式 if (arry.Contains(strEnd))//判断这个数组中是否有("jpg", "gif", "bmp", "png","jpeg","JPG","GIF","BMP","PNG","JPEG") { Random rand = new Random();//定义一个随机数,为了防止你要上传得图片重名 string strName = DateTime.Now.ToString("yyyymmmddhhss") + rand.Next(100, 9999).ToString();//得到不同得名字 string strPointEnd = "." + strEnd; string strFile = Server.MapPath("~/upfile");//获取其相对地址 FileUpload1.SaveAs(strFile + "/" + strName + strPointEnd);//保存原始图片 string src = strFile + "/" + strName + strPointEnd; //进行水印添加处理-------------------------------------------- //水印图片 string shuiyin = "~/watermark/logo.png"; //加载文件 System.Drawing.Image Cover; Cover = System.Drawing.Image.FromFile(src); //加载水印文件 System.Drawing.Image water = System.Drawing.Image.FromFile(Request.MapPath(shuiyin)); //创建画布 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(Cover); if (location == 1)//左上方 { //在image上绘制水印 g.DrawImage(water, new Rectangle(0, 0, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } else if (location == 2)//左下方 { //在image上绘制水印 g.DrawImage(water, new Rectangle(0, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } else if (location == 3)//右上方 { //在image上绘制水印 g.DrawImage(water, new Rectangle(Cover.Width - water.Width, 0, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } else if (location == 4)//右下方 { //在image上绘制水印 g.DrawImage(water, new Rectangle(Cover.Width - water.Width, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } else if (location == 5)//正中间 { //在image上绘制水印 g.DrawImage(water, new Rectangle((Cover.Width - water.Width) / 2, (Cover.Height - water.Height) / 2, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } else { //在image上绘制水印 g.DrawImage(water, new Rectangle(Cover.Width - water.Width, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel); } //释放画布 g.Dispose(); //释放水印图片 water.Dispose(); Cover.Save(HttpContext.Current.Server.MapPath("~/upfile/") + strName+"s"+ "." + strEnd);//保存打过水印的图片 Cover.Dispose(); string src1 = "upfile/" + strName + "s" + "." + strEnd; Response.Write("<a href="+src1+">点击查看水印图片</a>"); } else { Response.Write("<script>alert(''''该图片格式不能上传!'''')</script>"); } } } else { Response.Write("<script>alert(''''该图片格式不能上传!'''')</script>"); } } else { Response.Write("<script>alert(''''请选择图片路径!'''')</script>"); } }