首页 技术 正文
技术 2022年11月11日
0 收藏 547 点赞 2,325 浏览 4424 个字

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

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

转载地址:http://www.aspnetjia.com/Cont-39.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,059
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,728