Example #1
0
        private static MemoryStream GetAsciiArtStream(AsciiImageCoverterService asciiService, Bitmap bitmap)
        {
            var convertedAsciiArt = asciiService.ConvertImage(bitmap);

            byte[] asciiArtBytes = Encoding.UTF8.GetBytes(convertedAsciiArt);
            var    stream        = new MemoryStream(asciiArtBytes);

            return(stream);
        }
        public override void Run()
        {
            Trace.WriteLine("Starting processing of messages");

            // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump.
            Client.OnMessage((receivedMessage) =>
            {
                try
                {
                    var imageMessage = receivedMessage.GetBody <ImageMessage>();

                    var blobConnectionString = CloudConfigurationManager.GetSetting("BlobStorage.ConnectionString");
                    var storageAccount       = CloudStorageAccount.Parse(blobConnectionString);

                    var blobClient = storageAccount.CreateCloudBlobClient();
                    var container  = blobClient.GetContainerReference("images");
                    var blobBlock  = container.GetBlockBlobReference(imageMessage.BlobBlockName);

                    var stream = new MemoryStream();
                    blobBlock.DownloadToStream(stream);
                    stream.Position = 0;
                    var bitmap      = (Bitmap)Image.FromStream(stream);

                    var converterService = new AsciiImageCoverterService();

                    string result = converterService.ConvertImage(bitmap);

                    var convertedContainer = blobClient.GetContainerReference("converted-images");
                    convertedContainer.CreateIfNotExists();
                    convertedContainer.GetBlockBlobReference(imageMessage.BlobBlockName).UploadText(result);

                    // Process the message
                    Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString());
                }
                catch (Exception ex)
                {
                    // Handle any message processing specific exceptions here
                }
            });

            CompletedEvent.WaitOne();
        }
        private static MemoryStream GetAsciiArtStream(AsciiImageCoverterService asciiService, Bitmap bitmap)
        {
            var convertedAsciiArt = asciiService.ConvertImage(bitmap);

            byte[] asciiArtBytes = Encoding.UTF8.GetBytes(convertedAsciiArt);
            var stream = new MemoryStream(asciiArtBytes);
            return stream;
        }