예제 #1
0
        public CommonResult<CropImageResult> CropProfileImage(CropImageData cropImageData)
        {
            var originalFilePath = _configurationProvider.GetHostingEnvironmentWWWRootPath() + cropImageData.ImageUrl;

            OnBeforeCropImage(cropImageData);

            var fileName = CropImage(StaticManager.UserName, originalFilePath, cropImageData);

            return CommonResult<CropImageResult>.Success(new CropImageResult
            {
                Status = "success",
                Url = _configurationProvider.GetProfileCroppedImagesFolderInternalPath() + fileName
            });
        }
예제 #2
0
        private string CropImage(string userName, string originalFilePath, CropImageData cropImageData)
        {
            var originalImage = Image.FromFile(originalFilePath);

            var resizedOriginalImage = new Bitmap(originalImage, cropImageData.ImageScaledWidth, cropImageData.ImageScaledHeight);
            var targetImage = new Bitmap(cropImageData.ImageCropWidth, cropImageData.ImageCropHeight);

            using (var graphics = Graphics.FromImage(targetImage))
            {
                graphics.DrawImage(resizedOriginalImage, 
                    new Rectangle(0,
                                  0, 
                                  cropImageData.ImageCropWidth, 
                                  cropImageData.ImageCropHeight
                    ), 
                    new Rectangle(cropImageData.ImageCropStartPointX, 
                                  cropImageData.ImageCropStartPointY, 
                                  cropImageData.ImageCropWidth, 
                                  cropImageData.ImageCropHeight
                    ), 
                    GraphicsUnit.Pixel);
            }

            var fileName = Path.GetFileName(originalFilePath);
            var folder = _configurationProvider.GetProfileCroppedImagesFolderPath();
            var croppedPath = Path.Combine(folder, fileName);

            if (File.Exists(croppedPath))
            {
                croppedPath = Path.Combine(folder, Guid.NewGuid().ToString());
            }

            targetImage.Save(croppedPath);

            UpdateUserProfileImage(userName, fileName);

            return fileName;
        }
예제 #3
0
 private void OnBeforeCropImage(CropImageData cropImageData)
 {
     cropImageData.ImageScaledHeight = cropImageData.ImageScaledHeight == 0 ? cropImageData.ImageOriginalHeight : cropImageData.ImageScaledHeight;
 }