Пример #1
0
        private Response CreateCache()
        {
            var cache = new CacheModel();
            var imageModel = new ImageModel();

            var file = Request.Files.First();
            var original = new Bitmap(file.Value);
            var processed = ImageProcessor.ApplyFilters(original);

            imageModel.Id = Guid.NewGuid();
            imageModel.OriginalImageId = Guid.NewGuid().ToString();
            imageModel.OriginalImageThumbnailId = Guid.NewGuid().ToString();
            imageModel.ProcessedImageId = Guid.NewGuid().ToString();
            imageModel.ProcessedImageThumbnailId = Guid.NewGuid().ToString();

            cache.Id = Guid.NewGuid();
            cache.Images = new List<Guid>(new []{ imageModel.Id });
            cache.CreatorId = Guid.Empty;
            cache.CreatedDate = DateTime.Now;

            using (var ms = new MemoryStream(original.ToByteArray()))
                _documentStore.DatabaseCommands.PutAttachment(imageModel.OriginalImageId, null, ms, new RavenJObject());

            using (var ms = new MemoryStream(processed.ToByteArray()))
                _documentStore.DatabaseCommands.PutAttachment(imageModel.ProcessedImageId, null, ms, new RavenJObject());

            using (var ms = new MemoryStream(original.GenerateThumbnail(80, 80).ToByteArray()))
                _documentStore.DatabaseCommands.PutAttachment(imageModel.OriginalImageThumbnailId, null, ms, new RavenJObject());

            using (var ms = new MemoryStream(processed.GenerateThumbnail(80, 80).ToByteArray()))
                _documentStore.DatabaseCommands.PutAttachment(imageModel.ProcessedImageThumbnailId, null, ms, new RavenJObject());

            _images.Create(imageModel);
            _caches.Create(cache);

            return Response.AsJson(cache);
        }
Пример #2
0
        public virtual ActionResult Thumbnail(string galleryName, string path)
        {
            var gallery = GetGallery(galleryName);

            var fullPath = GetAndValidateFullPath(gallery, path);
            var cachePath = Path.Combine(gallery.ImageDir, THUMBNAIL_DIR, path);

            // Cache the thumbnail if it doesn't already exist
            if (!System.IO.File.Exists(cachePath))
            {
                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(cachePath));

                using (var sourceImg = new Bitmap(fullPath))
                using (var thumb = sourceImg.GenerateThumbnail(MAX_THUMBNAIL_SIZE))
                {
                    thumb.Save(cachePath, ImageFormat.Png);
                }
            }

            return File(cachePath, "image/png");
        }