示例#1
0
        public string GetDefaultPictureUrl(int targetSize = 0, PictureType defaultPictureType = PictureType.Entity)
        {
            string defaultImageName;

            switch (defaultPictureType)
            {
            case PictureType.Avatar:
                defaultImageName = _fileManagementSettings.DefaultAvatarImageName;
                break;

            default:
                defaultImageName = _fileManagementSettings.DefaultEntityImageName;
                break;
            }

            var relativePath = string.Concat(_webHelper.GetAppLocation(), _cdnSettings.ImagesPath, defaultImageName);

            if (targetSize == 0)
            {
                return(relativePath);
            }

            var filePath = Path.Combine(_fileManagementSettings.LocalImagePath, defaultImageName);

            if (File.Exists(filePath))
            {
                var fileExtension = Path.GetExtension(filePath);
                var fileName      = string.Format("{0}-{1}{2}", Path.GetFileNameWithoutExtension(filePath), targetSize, fileExtension);

                if (!File.Exists(Path.Combine(_fileManagementSettings.LocalThumbImagePath, fileName)))
                {
                    var bitmap = new Bitmap(filePath);

                    var newSize = _mediaUtils.CalculateDimensions(bitmap.Size, targetSize);

                    if (newSize.Width < 1)
                    {
                        newSize.Width = 1;
                    }

                    if (newSize.Height < 1)
                    {
                        newSize.Height = 1;
                    }

                    var newBitMap = new Bitmap(newSize.Width, newSize.Height);
                    var g         = Graphics.FromImage(newBitMap);
                    g.SmoothingMode      = SmoothingMode.HighQuality;
                    g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    g.DrawImage(bitmap, 0, 0, newSize.Width, newSize.Height);

                    var ep = new EncoderParameters();
                    ep.Param[0] = new EncoderParameter(Encoder.Quality, _fileManagementSettings.ImageQuality);
                    var ici = _mediaUtils.GetImageCodecInfoFromExtension(fileExtension) ?? _mediaUtils.GetImageCodecInfoFromMimeType("image/jpeg");

                    newBitMap.Save(Path.Combine(_fileManagementSettings.LocalThumbImagePath, fileName), ici, ep);
                    newBitMap.Dispose();
                    bitmap.Dispose();
                }
                return(string.Concat(_webHelper.GetAppLocation(), _fileManagementSettings.LocalThumbImagePath, fileName));
            }
            return(relativePath);
        }