示例#1
0
        private ResizeMode GetRezieMode(ThumMode mode)
        {
            var resizeMode = ResizeMode.Crop;

            switch (mode)
            {
            case ThumMode.Crop:
                resizeMode = ResizeMode.Crop;
                break;

            case ThumMode.Fit:
                resizeMode = ResizeMode.Max;
                break;

            case ThumMode.Pad:
                resizeMode = ResizeMode.Pad;
                break;

            default:
                break;
            }

            return(resizeMode);
        }
示例#2
0
        public string GetPicUrl(long attaId, int width = 100, int height = 100, ThumMode mode = ThumMode.Crop, int quality = 100, WaterMarkingPosition?waterPosition = null)
        {
            var atta = AttachmentRepository.Get(attaId);

            if (atta == null)
            {
                return(null);
            }
            var thumbPath = Path.Combine(SysConfigRepository.UploadThumbDirectory,
                                         $"{mode}_{width}x{height}",
                                         atta.FilePath);
            var thumbFullPath = GetFullPath(thumbPath);

            if (!File.Exists(thumbFullPath))
            {
                var originalFullPath = GetFullPath(atta.FilePath);
                if (!File.Exists(originalFullPath))
                {
                    return(null);
                }
                using (var originalImage = new ImageFactory(true))
                {
                    ResizeMode resizeMode  = GetRezieMode(mode);
                    var        resizeLayer = new ResizeLayer(new Size(width, height), resizeMode);
                    originalImage.Load(originalFullPath)
                    .Resize(resizeLayer);
                    AddWatermark(waterPosition, originalImage);
                    originalImage.BackgroundColor(Color.White)
                    .Quality(quality)
                    .Save(thumbFullPath);
                }
            }


            return(GetFileUrl(Path.Combine(SysConfigRepository.UploadRootDirectory, thumbPath)));
        }
        public static string ThumbUrl(this UrlHelper urlHelper, long picId, int width, int height, WaterMarkingPosition position, ThumMode mode = ThumMode.Crop, string defUrl = "")
        {
            if (picId == 0)
            {
                return(defUrl);
            }
            var attaSvc = DependencyResolver.Current.GetService <IAttachmentSvc>();

            return(attaSvc.GetPicUrl(picId, width, height, mode, 100, position));
        }