Esempio n. 1
0
        private void SaveComic(Fredin.Comic.Data.Comic comic, ComicModelContext entityContext)
        {
            // Generate comic
            ComicGenerator generator      = this.CreateComicGenerator(comic, false);
            ComicGenerator frameGenerator = this.CreateComicGenerator(comic, true);

            entityContext.SaveChanges();

            // Storage container for all renders
            CloudBlobContainer container = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.RenderContainer);

            // Push full size comic to render storage
            using (MemoryStream comicStream = new MemoryStream())
            {
                generator.ComicImage.Save(comicStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                comicStream.Seek(0, SeekOrigin.Begin);

                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.ComicDirectory);
                CloudBlob          comicBlob = directory.GetBlobReference(comic.StorageKey);
                comicBlob.Properties.ContentType  = "image/jpeg";
                comicBlob.Properties.CacheControl = RenderCacheControl;
                comicBlob.UploadFromStream(comicStream);
            }

            // Push thumb comic
            using (MemoryStream thumbStream = new MemoryStream())
            {
                ComicGenerator.CropImage(new Size(364, 113), generator.GenerateThumb(364), ComicGenerator.ImageAlign.Top)
                .Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                thumbStream.Seek(0, SeekOrigin.Begin);

                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.ThumbDirectory);
                CloudBlob          thumbBlob = directory.GetBlobReference(comic.StorageKey);
                thumbBlob.Properties.ContentType  = "image/jpeg";
                thumbBlob.Properties.CacheControl = RenderCacheControl;
                thumbBlob.UploadFromStream(thumbStream);
            }

            // Push 1st frame
            using (MemoryStream frameStream = new MemoryStream())
            {
                frameGenerator.ComicImage.Save(frameStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                frameStream.Seek(0, SeekOrigin.Begin);

                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.FrameDirectory);
                CloudBlob          frameBlob = directory.GetBlobReference(comic.StorageKey);
                frameBlob.Properties.ContentType  = "image/jpeg";
                frameBlob.Properties.CacheControl = RenderCacheControl;
                frameBlob.UploadFromStream(frameStream);
            }

            // Push 1st frame thumb
            using (MemoryStream frameThumbStream = new MemoryStream())
            {
                frameGenerator.GenerateThumb(150).Save(frameThumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                frameThumbStream.Seek(0, SeekOrigin.Begin);

                CloudBlobDirectory directory      = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.FrameThumbDirectory);
                CloudBlob          frameThumbBlob = directory.GetBlobReference(comic.StorageKey);
                frameThumbBlob.Properties.ContentType  = "image/jpeg";
                frameThumbBlob.Properties.CacheControl = RenderCacheControl;
                frameThumbBlob.UploadFromStream(frameThumbStream);
            }

            // Push all photos
            foreach (ComicPhoto photo in comic.ComicPhotos)
            {
                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.PhotoDirectory);
                CloudBlob          photoBlob = directory.GetBlobReference(photo.Photo.StorageKey);
                photoBlob.Properties.ContentType  = "image/jpeg";
                photoBlob.Properties.CacheControl = RenderCacheControl;
                photoBlob.UploadByteArray(photo.Photo.ImageData);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a comic generator and loads the appropriate images and text bubbles into it.
        /// </summary>
        private ComicGenerator CreateComicGenerator(Fredin.Comic.Data.Comic comic, bool firstFrameOnly)
        {
            CloudBlobContainer blobContainer = this.BlobClient.GetContainerReference("static");

            int firstFrameOrdinal = comic.ComicPhotos
                                    .Min(p => p.TemplateItem.Ordinal);

            ComicPhoto firstFrame = comic.ComicPhotos
                                    .First(p => p.TemplateItem.Ordinal == firstFrameOrdinal);

            // Watermark data
            CloudBlob watermarkBlob  = blobContainer.GetBlobReference("Image/watermark.png");
            Bitmap    watermarkImage = null;

            using (MemoryStream watermarkStream = new MemoryStream(watermarkBlob.DownloadByteArray()))
            {
                watermarkImage = new Bitmap(watermarkStream);
            }

            // Create generator
            ComicGenerator generator = null;

            if (firstFrameOnly)
            {
                generator = new ComicGenerator(firstFrame.TemplateItem.Width, firstFrame.TemplateItem.Height);
            }
            else
            {
                generator = new ComicGenerator(comic.Template.Width, comic.Template.Height + watermarkImage.Height);
                generator.AddScaleImage(watermarkImage, watermarkImage.Width, watermarkImage.Height, comic.Template.Width - watermarkImage.Width, comic.Template.Height);
            }

            // Add frame photos
            foreach (ComicPhoto item in comic.ComicPhotos)
            {
                if (firstFrameOnly && firstFrameOrdinal == item.TemplateItem.Ordinal)
                {
                    using (MemoryStream imageStream = new MemoryStream(item.Photo.ImageData))
                    {
                        Bitmap image = new Bitmap(imageStream);
                        generator.AddFitImage(image, item.TemplateItem.Width, item.TemplateItem.Height, 0, 0, item.Alignment);
                    }
                }
                else if (!firstFrameOnly)
                {
                    using (MemoryStream imageStream = new MemoryStream(item.Photo.ImageData))
                    {
                        Bitmap image = new Bitmap(imageStream);
                        generator.AddFitImage(image, item.TemplateItem.Width, item.TemplateItem.Height, item.TemplateItem.X, item.TemplateItem.Y, item.Alignment);
                    }
                }
            }

            foreach (ComicTextBubble comicBubble in comic.ComicTextBubbles)
            {
                if (!comicBubble.Position.IsEmpty)
                {
                    if (!firstFrameOnly || ((comicBubble.X < firstFrame.TemplateItem.X + firstFrame.TemplateItem.Width) && comicBubble.Y < firstFrame.TemplateItem.Y + firstFrame.TemplateItem.Height))
                    {
                        // Get text bubble image from storage
                        string    bubbleKey  = String.Format("Image/TextBubble/{0}-{1}.png", comicBubble.TextBubbleDirection.TextBubble.Title, comicBubble.TextBubbleDirection.Direction);
                        CloudBlob bubbleBlob = blobContainer.GetBlobReference(bubbleKey);

                        using (MemoryStream bubbleStream = new MemoryStream(bubbleBlob.DownloadByteArray()))
                        {
                            Bitmap bubbleImage = new Bitmap(bubbleStream);

                            // Scale background using base : text ratio
                            int width  = Convert.ToInt32(Convert.ToDouble(comicBubble.TextBubbleDirection.TextBubble.BaseScaleX) / Convert.ToDouble(comicBubble.TextBubbleDirection.TextBubble.TextScaleX) * (double)comicBubble.Position.Width);
                            int height = Convert.ToInt32(Convert.ToDouble(comicBubble.TextBubbleDirection.TextBubble.BaseScaleY) / Convert.ToDouble(comicBubble.TextBubbleDirection.TextBubble.TextScaleY) * (double)comicBubble.Position.Height);
                            int x      = comicBubble.Position.X - ((width - Convert.ToInt32(comicBubble.Position.Width)) / 2);
                            int y      = comicBubble.Position.Y - ((height - Convert.ToInt32(comicBubble.Position.Height)) / 2);

                            generator.AddScaleImage(bubbleImage, width, height, x, y);
                            generator.AddText(comicBubble.Text, new RectangleF(comicBubble.Position.X, comicBubble.Position.Y, comicBubble.Position.Width, comicBubble.Position.Height), comicBubble.Font);
                        }
                    }
                }
            }

            return(generator);
        }