示例#1
0
        public async static void ProcessQueueMessagePoison(
            [QueueTrigger("images2process-poison")] PictureProcessMessage processMessage
            )
        {
            int pictureKeyToProcess = processMessage.PictureKey;

            Console.WriteLine("[Poison] Deze blijft maar mislukken!:  " + pictureKeyToProcess);
        }
示例#2
0
        public async static Task ProcessQueueMessage(
            [QueueTrigger("images2process")] PictureProcessMessage processMessage,
            [Blob("images/L{PictureKey}.jpg", FileAccess.Read)] Stream input,
            [Blob("images/M{PictureKey}.jpg")] CloudBlockBlob mediumOutputBlob,
            [Blob("images/S{PictureKey}.jpg")] CloudBlockBlob smallOutputBlob
            )
        {
            int pictureKeyToProcess = processMessage.PictureKey;

            Console.WriteLine("ImageKey:  " + pictureKeyToProcess);

            var largeImage = Image.FromStream(input);

            var smallImage  = largeImage.Resize(200, 200);
            var mediumImage = largeImage.Resize(1024, 1024);

            using (var smallMemoryStream = smallOutputBlob.OpenWrite())
            {
                smallOutputBlob.Properties.ContentType = "image/jpeg";
                smallImage.Save(smallMemoryStream, ImageFormat.Jpeg);
            }

            using (var mediumMemoryStream = mediumOutputBlob.OpenWrite())
            {
                mediumOutputBlob.Properties.ContentType = "image/jpeg";
                mediumImage.Save(mediumMemoryStream, ImageFormat.Jpeg);
            }

            using (IArticleRepository articleRepo = new ArticleRepository())
            {
                var picture = await articleRepo.GetPictureByKeyAsync(pictureKeyToProcess);

                picture.UrlSmall  = smallOutputBlob.Uri.ToString();
                picture.UrlMedium = mediumOutputBlob.Uri.ToString();

                await articleRepo.UpdatePictureAsync(picture);
            }
        }
示例#3
0
        //public async Task<IHttpActionResult> CreateImage()
        public async Task <IEnumerable <PictureInfo> > CreateImage()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();

            provider = await Request.Content.ReadAsMultipartAsync();

            var pictures = new List <PictureInfo>();

            using (IArticleRepository articleRepo = new ArticleRepository())
            {
                foreach (HttpContent content in provider.Contents)
                {
                    Stream stream = await content.ReadAsStreamAsync();

                    Image imageLarge = Image.FromStream(stream);

                    Picture picture = await articleRepo.AddPictureAsync();

                    //Resize and store in blov storage
                    var testName = content.Headers.ContentDisposition.Name;

                    //Retrieve storage account from connection string.
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                        CloudConfigurationManager.GetSetting("StorageConnectionString"));

                    //Create the blob client.
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    //Retrieve a reference to a container.
                    CloudBlobContainer imagesContainer = blobClient.GetContainerReference("images");

                    //Create the container if it doesn't already exist.
                    imagesContainer.CreateIfNotExists();

                    imagesContainer.SetPermissions(new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });

                    CloudBlockBlob imageBlob = imagesContainer.GetBlockBlobReference(string.Format("L{0}.jpg", picture.PictureKey));
                    using (var imageStream = new MemoryStream())
                    {
                        imageLarge.Save(imageStream, ImageFormat.Jpeg);
                        imageStream.Position = 0;

                        imageBlob.Properties.ContentType = "image/jpeg";
                        await imageBlob.UploadFromStreamAsync(imageStream);

                        var pictureInfo = new PictureInfo()
                        {
                            PictureKey = picture.PictureKey, UriLarge = imageBlob.Uri.ToString()
                        };

                        pictures.Add(pictureInfo);

                        picture.UrlLarge = pictureInfo.UriLarge;
                    }

                    await articleRepo.UpdatePictureAsync(picture);

                    //Create the queue client.
                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

                    //Retrieve a reference to a queue.
                    CloudQueue queue = queueClient.GetQueueReference("images2process");

                    //Create the queue if it doesn't already exist.
                    queue.CreateIfNotExists();

                    var pictureProcessMessage = new PictureProcessMessage()
                    {
                        PictureKey = picture.PictureKey
                    };
                    //var pictureProcessMessageJson = await JsonConvert.SerializeObjectAsync(pictureProcessMessage);
                    var pictureProcessMessageJson = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(pictureProcessMessage));

                    //send message to queue
                    var message = new CloudQueueMessage(pictureProcessMessageJson);
                    await queue.AddMessageAsync(message);
                }
            }

            return(pictures);
        }