• 图片压缩以及解析方法


    图片压缩

    1.调用:var result = PictureService.Upload(System.Web.HttpContext.Current.Request, 20);

    2.定义:

    public static FileResult Upload(HttpRequest request, int fileSize)
            {
                var result = new FileResult { IsError = true };

                fileSize = 10 * 1024 * 1024;
                //const int fileSize = 10 * 1024 * 1024;

                var files = request.Files;

                //定义允许上传的文件扩展名
                var extType = new Hashtable
                            {
                                {"image", "gif,jpg,jpeg,png,bmp"},
                                {"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,apk"}
                            };
                //验证文件类型,是image或者file
                var dirName = request.Form["fileType"];
                if (string.IsNullOrEmpty(dirName))
                {
                    dirName = "image";
                }

                if (files.Count < 0)
                {
                    result.ErrMsg = "请选择文件。";
                    return result;
                }
                else
                {
                    for (var i = 0; i < files.Count; i++)
                    {
                        var fileName = files[i].FileName;
                        var fileExt = Path.GetExtension(fileName).ToLower();

                        if (files[i].InputStream.Length > fileSize)
                        {
                            result.ErrMsg = "上传文件大小超过限制。";
                            return result;
                        }
                        if (string.IsNullOrWhiteSpace(fileExt))
                        {
                            result.ErrMsg = "上传文件扩展名是不允许的扩展名。 只允许" + ((string)extType[dirName]) + "格式。";
                            return result;
                        }

                        var imageBytes = new byte[files[i].ContentLength];

                        files[i].InputStream.Read(imageBytes, 0, imageBytes.Length);
                        files[i].InputStream.Close();

                        //获取文件保存路径
                        var savePath = string.IsNullOrWhiteSpace(request.Form["path"]) ? "H5Default" : request.Form["path"];
                        switch (savePath)
                        {
                            case "H5Default":
                                savePath = UploadPathEnum.H5Default.ToString();
                                break;
                            case "H5Circle":
                                savePath = UploadPathEnum.H5Circle.ToString();
                                break;
                            case "H5Diary":
                                savePath = UploadPathEnum.H5Diary.ToString();
                                break;
                            case "H5Product":
                                savePath = UploadPathEnum.H5Product.ToString();
                                break;
                            case "H5Advert":
                                savePath = UploadPathEnum.H5Advert.ToString();
                                break;
                            case "News":
                                savePath = "News";
                                break;
                            default:
                                result.ErrMsg = "上传文件目录不存在。";
                                return result;
                        }

                        result = dirName == "image" ? UploadImg(request, imageBytes, savePath) : UploadFile(request, imageBytes, savePath);
                    }
                }
                return result;
            }

    3.Upload Img:

        /// <summary>
            /// 上传图片方法
            /// </summary>
            /// <param name="request">请求</param>
            /// <param name="imageBytes">图片大小</param>
            /// <param name="savePath">上传路径</param>
            /// <returns></returns>
            protected static FileResult UploadImg(HttpRequest request, byte[] imageBytes, string savePath)
            {
                var result = new FileResult { IsError = true };
                try
                {
                    Guid pid;
                    var load = UploadHelper.UpLoadImg(imageBytes, savePath, out pid);
                    if (!load.IsError)
                    {
                        //var flag = InsertData(request, load.Data.ToString(), pid);

                        result.Data = load.Data;
                        result.IsError = false;
                    }
                }
                catch (Exception ex)
                {
                    result.ErrMsg = ex.Message;
                }
                return result;
            }

    4.图片缩略

    public static FileResult UpLoadImg(byte[] imageByte, string direetory, out Guid pid)
            {
                var result = new FileResult() { IsError = true };
                pid = Guid.NewGuid();

                try
                {
                    var soapClient = new AjaxImgSoapClient();
                    //调用服务上传图片

                    var filepath = string.Empty;

                    switch (direetory)
                    {
                        case "H5Default":
                            //case "H5Advert":
                            filepath = soapClient.UpLoadImg(imageByte, direetory, pid.ToString());
                            break;
                        case "H5Advert":
                            filepath = soapClient.UpLoadImgSaveThumbnail(imageByte, direetory, false, false);
                            break;
                        default:
                            filepath = soapClient.UpLoadImgSaveThumbnail(imageByte, direetory, true, true);
                            break;
                    }

                    if (string.IsNullOrWhiteSpace(filepath))
                    {
                        result.ErrMsg = "上传图片失败!";
                        return result;
                    }
                    else
                    {
                        result.Data = filepath;
                    }

                    result.IsError = false;
                }
                catch (Exception ex)
                {
                    result.ErrMsg = ex.Message;
                }
                return result;
            }

    5 解析:

    pics = base.GetThumbnailPicture(pics, Thumbnail.FourHundred);

     /// <summary>
            /// 获取新的缩略图图片路径【路径例子:http://files.meb.com//Pictures/H5/Product/20150828112321116.jpg】
            /// </summary>
            /// <param name="sourceImagePath">原图片路径</param>
            /// <param name="thumbnail">需要生成的图片缩略图大小</param>
            /// <param name="outSourceImagePath">图片原路径[如果此图片路径处理不正确,这里会进行相应处理,并返回]</param>
            /// <returns></returns>
            public string GetThumbnailPicture(string sourceImagePath, Thumbnail thumbnail)
            {
                if (sourceImagePath == null)
                {
                    return string.Empty;
                }
                //return ImageSite.TrimEnd('/') + "/" + sourceImagePath.Replace(ImageSite, string.Empty).Replace(FilesDomain, string.Empty).TrimStart('/');
                if (!sourceImagePath.ToUpper().Contains("NEWS") && !sourceImagePath.ToUpper().Contains("PRODUCT"))
                {
                    return ImageSite.TrimEnd('/') + "/" + sourceImagePath.Replace(ImageSite, string.Empty).Replace(FilesDomain, string.Empty).TrimStart('/').Replace(AliImageSite, string.Empty).TrimStart('/');
                }
                string outSourceImagePath = sourceImagePath;
                if (string.IsNullOrEmpty(sourceImagePath) || thumbnail == Thumbnail.Default) { return sourceImagePath; }
                //移除全路径部分
                outSourceImagePath = outSourceImagePath.Replace(ImageSite, string.Empty).Replace(FilesDomain, string.Empty).Replace(AliImageSite, string.Empty).TrimStart('/');
                //获取最后级目录索引
                int lastDictionaryIndex = outSourceImagePath.LastIndexOf("/");
                //文件名
                string headDictionary = string.Empty;
                string fileName = string.Empty;
                string thumbnailDictionary = string.Empty;
                if (lastDictionaryIndex > -1)
                {
                    headDictionary = outSourceImagePath.Substring(0, lastDictionaryIndex);
                    fileName = outSourceImagePath.Substring(lastDictionaryIndex);
                }
                if (thumbnail == Thumbnail.FourHundred)
                {
                    thumbnailDictionary = "400_400";
                }
                if (thumbnail == Thumbnail.SixHundred)
                {
                    thumbnailDictionary = "600_600";
                }
                if (thumbnail == Thumbnail.MoreThanOneThousand)
                {
                    thumbnailDictionary = "1250_1250";
                }
                //如果最后一级目录包含default则替换为空,连接上目标尺寸目录。如果不存在则直接连接目标目录
                if (headDictionary.ToLower().LastIndexOf("default") >= 0)
                {
                    headDictionary = headDictionary.ToLower().Substring(0, headDictionary.ToLower().LastIndexOf("default"));
                }
                //组建新路径
                return ImageSite.TrimEnd('/') + "/" + headDictionary.TrimStart('/').TrimEnd('/') + "/" + thumbnailDictionary.TrimStart('/').TrimEnd('/') + "/" + fileName.TrimStart('/');
            }

  • 相关阅读:
    区块链|肖臻《区块链技术与应用》公开课之以太坊篇
    区块链|肖臻《区块链技术与应用》公开课之比特币篇
    复合数据类型
    广播变量和累加器
    mysql_基本操作
    8.10 NOI模拟赛 fzhtql SAM 后缀数组 启发式合并 dsu on tree 树状数组 set 线段树合并
    心态
    7.29 NOI 模拟赛 Stars 状压 dp
    8.2 NOI模拟赛 Palindrome dp 组合计数 分类讨论
    7.30 NOI模拟赛 B Easy Sum 分块 NTT
  • 原文地址:https://www.cnblogs.com/special-tao/p/4916536.html
Copyright © 2020-2023  润新知