private FileContentResult CreateThumbnail(string physicalPath)
        {
            using (var fileStream = System.IO.File.OpenRead(physicalPath))
            {
                var desiredSize = new ImageSize
                {
                    Width  = ThumbnailWidth,
                    Height = ThumbnailHeight
                };

                return(File(_thumbnailCreator.Create(fileStream, desiredSize, ImageContentType), ImageContentType));
            }
        }
示例#2
0
        public RecipeStepBase(IBaseSettings settings, ILogger logger)
        {
            this._settings = settings;
            this._logger   = logger;

            this.Photo.Where(x => x != null).Subscribe(x => {
                using (var crypto = new SHA256CryptoServiceProvider()) {
                    var filePath             = string.Join("", crypto.ComputeHash(x).Select(b => $"{b:X2}")) + ".png";
                    this.PhotoFilePath.Value = filePath;

                    this.Thumbnail.Value         = ThumbnailCreator.Create(x, 100, 100);
                    var thumbFilePath            = string.Join("", crypto.ComputeHash(this.Thumbnail.Value).Select(b => $"{b:X2}")) + ".png";
                    this.ThumbnailFilePath.Value = thumbFilePath;
                }
            });

            this.PhotoFilePath.Where(x => x != null).ObserveOnDispatcher(DispatcherPriority.Background).ObserveOn(TaskPoolScheduler.Default).Subscribe(x => {
                var fullpath = Path.Combine(this._settings.GeneralSettings.ImageDirectoryPath, x);

                if (!File.Exists(fullpath))
                {
                    File.WriteAllBytes(fullpath, this.Photo.Value);
                }
                else
                {
                    this.Photo.Value = File.ReadAllBytes(fullpath);
                }
            });

            this.ThumbnailFilePath.Where(x => x != null).ObserveOnDispatcher(DispatcherPriority.Background).ObserveOn(TaskPoolScheduler.Default).Subscribe(x => {
                var fullpath = Path.Combine(this._settings.GeneralSettings.ImageDirectoryPath, x);
                if (this.Thumbnail.Value != null && !File.Exists(fullpath))
                {
                    File.WriteAllBytes(fullpath, this.Thumbnail.Value);
                }
                else
                {
                    this.Thumbnail.Value = File.ReadAllBytes(fullpath);
                }
            });
        }
        public ActionResult Thumbnail(string path)
        {
            //TODO:Add security checks

            var files = new FilesRepository();
            var image = files.ImageByPath(path);

            if (image != null)
            {
                var desiredSize = new ImageSize {
                    Width = ThumbnailWidth, Height = ThumbnailHeight
                };

                const string contentType = "image/png";

                var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());

                using (var stream = new MemoryStream(image.Image1.ToArray()))
                {
                    return(File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType));
                }
            }
            throw new HttpException(404, "File Not Found");
        }