• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#图片添加水印

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

今天讲一个上传图片添加水印的方法,直接上代码吧

 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>");
        }
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
关于替换“c2a0”十六进制字符的方法发布时间:2022-07-14
下一篇:
C#将普通字符串转换为二进制字符串发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap