Ejemplo n.º 1
0
    /// <summary>
    /// 裁剪图片并保存
    /// </summary>
    public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
    {
        string fileExt = SimonUtils.GetFileExt(fileName); //文件扩展名,不含“.”

        if (!IsImage(fileExt))
        {
            return(false);
        }
        string newFileDir = SimonUtils.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") + 1));

        //检查是否有该路径,没有则创建
        if (!Directory.Exists(newFileDir))
        {
            Directory.CreateDirectory(newFileDir);
        }
        try
        {
            string fileFullPath   = SimonUtils.GetMapPath(fileName);
            string toFileFullPath = SimonUtils.GetMapPath(newFileName);
            return(SimonThumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y));
        }
        catch
        {
            return(false);
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 文件上传方法
    /// </summary>
    /// <param name="postedFile">文件流</param>
    /// <param name="isThumbnail">是否生成缩略图</param>
    /// <param name="isWater">是否打水印</param>
    /// <returns>上传后文件信息</returns>
    public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater)
    {
        try
        {
            string fileExt              = SimonUtils.GetFileExt(postedFile.FileName);                               //文件扩展名,不含“.”
            int    fileSize             = postedFile.ContentLength;                                                 //获得文件大小,以字节为单位
            string fileName             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
            string newFileName          = SimonUtils.GetRamCode() + "." + fileExt;                                  //随机生成新的文件名
            string newThumbnailFileName = "thumb_" + newFileName;                                                   //随机生成缩略图文件名
            string upLoadPath           = GetUpLoadPath();                                                          //上传目录相对路径
            string fullUpLoadPath       = SimonUtils.GetMapPath(upLoadPath);                                        //上传目录的物理路径
            string newFilePath          = upLoadPath + newFileName;                                                 //上传后的路径
            string newThumbnailPath     = upLoadPath + newThumbnailFileName;                                        //上传后的缩略图路径

            //检查文件扩展名是否合法
            if (!CheckFileExt(fileExt))
            {
                return("不允许上传" + fileExt + "类型的文件");
            }
            //检查文件大小是否合法
            if (!CheckFileSize(fileExt, fileSize))
            {
                return("文件超过限制的大小");
            }
            //检查上传的物理路径是否存在,不存在则创建
            SimonUtils.CreateDir(fullUpLoadPath);

            //保存文件
            postedFile.SaveAs(fullUpLoadPath + newFileName);
            //如果是图片,检查图片是否超出最大尺寸,是则裁剪
            if (IsImage(fileExt) && (Upload_PicMaxWidth > 0 || Upload_PicMaxHeight > 0))
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                                                  Upload_PicMaxWidth, Upload_PicMaxHeight);
            }
            //如果是图片,检查是否需要生成缩略图,是则生成
            if (IsImage(fileExt) && isThumbnail && Upload_PicThumbnailWidth > 0 && Upload_PicThumbnailHeight > 0)
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                                                  Upload_PicThumbnailWidth, Upload_PicThumbnailHeight, "Cut");
            }
            //如果是图片,检查是否需要打水印
            if (IsWaterMark(fileExt) && isWater)
            {
                switch (Upload_PicWaterMarkType)
                {
                case 1:
                    SimonWaterMark.AddImageSignText(newFilePath, newFilePath, Upload_PicWaterMark_Txt, 9, 63, "黑体", 16);
                    break;

                case 2:
                    SimonWaterMark.AddImageSignPic(newFilePath, newFilePath, Upload_PicWaterMark_PicUrl, 9, 63, 6);
                    break;
                }
            }
            //处理完毕,返回JOSN格式的文件信息
            //return "{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
            //    + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
            //    + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}";
            return("success|" + newFilePath + "");
        }
        catch
        {
            return("上传过程中发生意外错误");
        }
    }