Пример #1
0
        public FileInfo SaveAs()
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            FileInfo           info  = null;

            try
            {
                for (int i = 0; i < files.Count; i++)
                {
                    this.postedFile    = files[i];
                    this.localFilePath = this.postedFile.FileName;
                    if ((this.localFilePath == null) || (this.localFilePath == ""))
                    {
                        throw new Exception("不能上传空文件");
                    }
                    this.localFileLength = this.postedFile.ContentLength;
                    if (this.localFileLength >= (this.sizes * 0x400))
                    {
                        throw new Exception("上传的文件不能大于:" + this.sizes + "KB");
                    }
                    this.saveFileFolderPath = this.GetSaveFileFolderPath();
                    this.localFileName      = System.IO.Path.GetFileName(this.postedFile.FileName);
                    this.fileExtension      = FileHelper.GetFileExtension(this.localFileName);
                    if (this.fileType.ToLower().IndexOf(this.fileExtension.ToLower()) == -1)
                    {
                        throw new Exception("目前本系统支持的格式为:" + this.fileType);
                    }
                    this.saveFileName     = FileHelper.CreateFileName(this.fileNameType, this.localFileName, this.fileExtension);
                    this.saveFileFullPath = this.saveFileFolderPath + this.saveFileName;
                    this.postedFile.SaveAs(this.saveFileFullPath);


                    if (allImageIsNail == 1)                                                                                                                                                                   //如果网站设置开启压缩图片
                    {
                        if (this.fileExtension == ".jpg" || this.fileType.ToLower() == ".jpeg" || this.fileType.ToLower() == ".gif" || this.fileType.ToLower() == ".png" || this.fileType.ToLower() == ".bmp") //如果后缀名为图片格式则进行压缩
                        {
                            Image image = Image.FromFile(this.saveFileFullPath);
                            if (image.Width > maxWidth)//如果图片超出设置的编辑器图片宽度,将图片压缩
                            {
                                string filePath = this.saveFileFolderPath + this.saveFileName;
                                string nailPath = this.saveFileFolderPath + "temp_nail" + this.fileExtension;
                                ImageHelper.MakeThumbnailImage(this.saveFileFullPath, nailPath, maxWidth, maxWidth, ThumbnailType.WidthFix);
                                image.Dispose();
                                System.IO.File.Delete(filePath);         //删除原图
                                System.IO.File.Move(nailPath, filePath); //重命名压缩后的文件
                            }
                            image.Dispose();
                        }
                    }

                    info = new FileInfo(this.saveFileFolderPath + this.saveFileName);
                }
            }
            catch
            {
                throw;
            }
            return(info);
        }
Пример #2
0
        public FileInfo SaveAs(int waterType, int waterPossition, string text, string textFont, int textSize, string textColor, string waterPhoto)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            FileInfo           info  = null;

            try
            {
                for (int i = 0; i < files.Count; i++)
                {
                    this.postedFile    = files[i];
                    this.localFilePath = this.postedFile.FileName;
                    if ((this.localFilePath == null) || (this.localFilePath == ""))
                    {
                        throw new Exception("不能上传空文件");
                    }
                    this.localFileLength = this.postedFile.ContentLength;
                    if (this.localFileLength >= (this.sizes * 0x400))
                    {
                        throw new Exception("上传的文件不能大于:" + this.sizes + "KB");
                    }
                    this.saveFileFolderPath = this.GetSaveFileFolderPath();
                    this.localFileName      = System.IO.Path.GetFileName(this.postedFile.FileName);
                    this.fileExtension      = FileHelper.GetFileExtension(this.localFileName);
                    if (this.fileType.ToLower().IndexOf(this.fileExtension.ToLower()) == -1)
                    {
                        throw new Exception("目前本系统支持的格式为:" + this.fileType);
                    }
                    this.saveFileName     = FileHelper.CreateFileName(this.fileNameType, this.localFileName, this.fileExtension);
                    this.saveFileFullPath = this.saveFileFolderPath + this.saveFileName;
                    this.postedFile.SaveAs(this.saveFileFullPath);

                    var imgTypes = ".jpg|.gif|.bmp|.png|.jpeg";
                    if (imgTypes.IndexOf(this.fileExtension.ToLower()) != -1) //必须是图片类型才能加压缩
                    {
                        if (allImageIsNail == 1)                              //如果网站设置开启压缩图片
                        {
                            Image image = Image.FromFile(this.saveFileFullPath);
                            if (image.Width > maxWidth)//如果图片超出设置的编辑器图片宽度,将图片压缩
                            {
                                string filePath = this.saveFileFolderPath + this.saveFileName;
                                string nailPath = this.saveFileFolderPath + "temp_nail" + this.fileExtension;
                                ImageHelper.MakeThumbnailImage(this.saveFileFullPath, nailPath, maxWidth, maxWidth, ThumbnailType.WidthFix);
                                image.Dispose();
                                System.IO.File.Delete(filePath);         //删除原图
                                System.IO.File.Move(nailPath, filePath); //重命名压缩后的文件
                            }
                            image.Dispose();
                        }

                        //添加水印
                        string sFileName = System.IO.Path.GetFileNameWithoutExtension(saveFileName) + "_wm" + this.fileExtension;
                        string newPath   = System.IO.Path.Combine(this.saveFileFolderPath, sFileName);
                        if (waterType == 2)
                        {
                            ImageHelper.AddTextWater(saveFileFullPath, newPath, waterPossition, text, textFont, textColor, textSize);
                        }
                        else
                        {
                            ImageHelper.AddImageWater(saveFileFullPath, newPath, waterPossition, waterPhoto);
                        }
                        //删除没上水印的老图
                        //if (System.IO.File.Exists(saveFileFullPath))
                        //{
                        //    System.IO.File.Delete(saveFileFullPath);
                        //}

                        info = new FileInfo(newPath);
                    }
                }
            }
            catch
            {
                throw;
            }
            return(info);
        }