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); } }
private void ExecuteTask(object state) { CloudQueueMessage queueMessage = (CloudQueueMessage)state; this.Log.DebugFormat("Executing image task {0}", queueMessage.AsString); string connectionString = ConfigurationManager.ConnectionStrings["ComicModelContext"].ConnectionString; ComicModelContext entityContext = new ComicModelContext(connectionString); PhotoTask task = null; try { // Read task details from storage CloudBlobContainer container = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.TaskContainer); CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.PhotoTaskDirectory); CloudBlob blob = directory.GetBlobReference(queueMessage.AsString); XmlSerializer serializer = new XmlSerializer(typeof(PhotoTask)); using (MemoryStream stream = new MemoryStream()) { blob.DownloadToStream(stream); stream.Seek(0, SeekOrigin.Begin); task = (PhotoTask)serializer.Deserialize(stream); task.Status = TaskStatus.Executing; this.UpdateTask(task); } User user = entityContext.TryGetUser(task.OwnerUid); FacebookClient facebook = new FacebookClient(task.FacebookToken); // Get image from web Bitmap image = this.GetImage(task.SourceUrl); if (task.Effect != ComicEffectType.None) { // Apply render effect RenderHelper effectHelper = new RenderHelper(image.Size); // Translate intensity to render parameters using min / max range Dictionary <string, object> parameters = new Dictionary <string, object>(); foreach (RenderParameter p in effectHelper.GetRenderParameters(task.Effect)) { if (task.Intensity == 0) { parameters.Add(p.Name, p.MinValue); } else if (task.Intensity == 1) { parameters.Add(p.Name, p.DefaultValue); } else if (task.Intensity == 2) { parameters.Add(p.Name, p.MaxValue); } } ImageRenderData renderResult = effectHelper.RenderEffect(image, task.Effect, parameters); image = new Bitmap(renderResult.RenderStream); } // Read raw photo into memory MemoryStream imageStream = new MemoryStream(); image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); imageStream.Seek(0, SeekOrigin.Begin); // Save to storage Photo photo = new Photo(); photo.CreateTime = DateTime.Now; photo.User = user; photo.StorageKey = Guid.NewGuid().ToString(); photo.Width = image.Width; photo.Height = image.Height; entityContext.AddToPhotos(photo); entityContext.SaveChanges(); CloudBlobContainer imageContainer = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.RenderContainer); CloudBlobDirectory imageDirectory = imageContainer.GetDirectoryReference(ComicConfigSectionGroup.Blob.PhotoDirectory); CloudBlob imageBlob = imageDirectory.GetBlobReference(photo.StorageKey); imageBlob.Properties.ContentType = "image/jpeg"; imageBlob.UploadFromStream(imageStream); // Mark task as completed task.PhotoId = photo.PhotoId; task.Status = TaskStatus.Complete; this.UpdateTask(task); this.Log.DebugFormat("Completed image task {0}", task.TaskId); } catch (Exception x) { this.Log.Error("Unable to complete image task.", x); if (task != null) { task.Status = TaskStatus.Failed; this.UpdateTask(task); } } }