国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c#圖片添加水印

2019-11-14 13:32:03
字體:
來源:轉載
供稿:網友

今天講一個上傳圖片添加水印的方法,直接上代碼吧

  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("<scr                }
                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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高碑店市| 上蔡县| 军事| 雅安市| 安塞县| 曲阜市| 克山县| 黄骅市| 崇义县| 中方县| 无为县| 湖北省| 错那县| 孟州市| 宝坻区| 金湖县| 濮阳县| 彰化市| 鄄城县| 界首市| 禹城市| 洛隆县| 卢湾区| 长宁县| 栖霞市| 兰溪市| 长顺县| 诏安县| 崇阳县| 孟村| 江北区| 佛冈县| 庆元县| 苍南县| 龙井市| 荣昌县| 大兴区| 嘉兴市| 常熟市| 清水县| 武宁县|