上传图片加水印

#region 上传文件
    /// <summary>
    /// 文件上传
    /// </summary>
    /// <returns>文件上传到服务器的路径</returns>
    void upfile()
    {
        string strWidth = "";
        Random myrd = new Random();
        string path, filename;    //上传文件路径,上传文件名称
        path = Server.MapPath("../../upfile/old/");

        filename = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "").Replace(":", "").Replace(" ", "") + myrd.Next(1000).ToString();        //上传文件名称

        //取得文件的扩展名
        string fileExtension = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower();
        path = path + filename + fileExtension;
        if (fileExtension != ".jpg" && fileExtension != ".gif" && fileExtension != ".jpeg"&&fileExtension!=".png")
        {
            JScript.Alert("错误!!文件类型必须为jpg或者gif!");           
        }
        else
        {
            File1.PostedFile.SaveAs(path);
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            int w = image.Width;
            strWidth = (w >= 600) ? " width=/"600/" " : "";
            int h = image.Height;
            g.DrawImage(image, 0, 0, w, h);
            w = GetFontSize(w);
            System.Drawing.Font f = new System.Drawing.Font("verdana", w);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.White);
            string addtext = "图片来自:http://www.Newboy2010.gicp.com(永远在一起)";
            h = h - GetFontHeight(h);
            g.DrawString(addtext, f, b, 0, h);
            g.Dispose();

            //保存加水印过后的图片
            string newpath = Server.MapPath("../../upfile/users/") + filename + fileExtension;
            image.Save(newpath);
            image.Dispose();
            string strFileName = "upfile/users/" + filename + fileExtension;
            string js = @"<script language=javascript>";
            js += " window.returnValue =  '<img src= /"" + strFileName + "/" " + strWidth + "/>';";
            js += "window.close();";
            js += "</script>";
            Response.Write(js);       
        }        
    }

    public bool ThumbnailCallback()
    {
        return false;
    }   

    private int GetFontHeight(int intHeight)
    {
        if (intHeight <= 100)
            return 10;
        if (intHeight > 100 && intHeight <= 200)
            return 15;
        else if (intHeight > 200 && intHeight <= 300)
            return 20;
        else if (intHeight > 300 && intHeight <= 400)
            return 25;
        else if (intHeight > 400 && intHeight <= 500)
            return 30;
        else if (intHeight > 500 && intHeight <= 600)
            return 35;
        else if (intHeight > 600 && intHeight <= 800)
            return 40;
        else
            return 50;
    }

    private int GetFontSize(int intWidth)
    {
        if (intWidth <= 100)
            return 4;
        else if (intWidth > 100 && intWidth <= 200)
            return 6;
        else if (intWidth > 200 && intWidth <= 300)
            return 8;
        else if (intWidth > 300 && intWidth <= 400)
            return 10;
        else if (intWidth > 400 && intWidth <= 500)
            return 12;
        else if (intWidth > 500 && intWidth <= 600)
            return 14;
        else if (intWidth > 600 && intWidth <= 800)
            return 16;
        else
            return 20;
    }

    #endregion

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值