Beispiel #1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);//用上传控件加载图片
            //System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/bb.jpg"));//用加字件加载图片

            string fileName  = Server.MapPath("/aa.jpg");
            string waterName = Server.MapPath("/lib/images/water.jpg");

            //图片水印
            // ImageWaterMark.AddImageSignPic(img, fileName, waterName, 9, 80, 6);
            //文字水印
            ImageWaterMark.AddImageSignText(img, fileName, "yunstudio", 9, 100, "微软雅黑", 50);
            Image1.ImageUrl = fileName;
        }
Beispiel #2
0
    ///<summary>
    /// 文件上传方法
    /// </summary>
    public string fileSaveAs(HttpPostedFile _postedFile, int _isWater)
    {
        try
        {
            string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
            //验证合法的文件
            if (!CheckFileExt(this.fileType, _fileExt))
            {
                return("{\"msg\": 1, \"msbox\": \"不允许上传" + _fileExt + "类型的文件!\"}");
            }
            if (this.fileSize > 0 && _postedFile.ContentLength > fileSize * 1024)
            {
                return("{\"msg\": 1, \"msbox\": \"文件超过限制的大小啦!\"}");
            }
            string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + _fileExt;     //随机文件名
            //检查保存的路径 是否有/开头结尾
            if (this.filePath.StartsWith("/") == false)
            {
                this.filePath = "/" + this.filePath;
            }
            if (this.filePath.EndsWith("/") == false)
            {
                this.filePath = this.filePath + "/";
            }
            //按日期归类保存
            string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
            this.filePath += _datePath;
            //获得要保存的文件路径
            string serverFileName = this.filePath + _fileName;
            //物理完整路径
            string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
            //检查是否有该路径没有就创建
            if (!Directory.Exists(toFileFullPath))
            {
                Directory.CreateDirectory(toFileFullPath);
            }
            //将要保存的完整文件名
            string toFile = toFileFullPath + _fileName;
            //保存文件
            _postedFile.SaveAs(toFile);

            //是否打图片水印
            if (isWatermark > 0 && _isWater == 1 && CheckFileExt("BMP|JPEG|JPG|GIF|PNG|TIFF", _fileExt))
            {
                switch (isWatermark)
                {
                case 1:
                    ImageWaterMark.AddImageSignText(serverFileName, this.filePath + _fileName, this.textWater, waterStatus, waterQuality, textWaterFont, textFontSize);
                    break;

                case 2:
                    ImageWaterMark.AddImageSignPic(serverFileName, this.filePath + _fileName, this.imgWaterPath, waterStatus, waterQuality, waterTransparency);
                    break;
                }
            }
            return("{\"msg\": 1, \"msbox\": \"" + serverFileName + "\"}");
        }
        catch
        {
            return("{\"msg\": 1, \"msbox\": \"上传过程中发生意外错误!\"}");
        }
    }