As Steve Porter mentioned in his blog post, How Wintellect Uses Microsoft Azure, we are making pretty heavy use of many offerings in the Azure toolset for our internal and client-facing web properties here at Wintellect and have been doing so from the early days of Azure. As we continue to experiment with new and/or improved Azure offerings, I thought it might be helpful to share some of the more interesting things we’ve worked through as we explore that ever-increasing world that is Azure.
As the first sample application for these Azure Bits posts, I will create a small image manipulator example in Azure. This example will allow me to demonstrate several pieces of the Azure stack working together, including an Azure hosted web app, Azure Blob Storage, an Azure Queue, and an Azure hosted WebJob.
Azure Image Manipulator
For this first version of the Azure Image Manipulator, the user will only be able to browse and select an image from their computer and submit the image for processing. When the user submits the image, the original image will be inserted into Azure Blob Storage and a message will be placed in an Azure Queue indicating that a new image has been uploaded and needs to have a thumbnail created. In follow-up posts, I will create a WebJob that monitors the Azure Queue and when a new image message is detected, it will process the message from the queue, fetch the original image from Azure Blob Storage, and create a manipulated (thumbnail) copy and save this new image to Azure Blob Storage with a known URL.
I’m keeping it very simple for this first revision and only creating a single hard-coded thumbnail at 200 x 300 pixels. That way, I can focus on the Azure moving parts and not get lost in how many cool options I can allow on the manipulated image. Once I have the basic pipeline in place for uploading and background processing of these images, I could add functionality such as allowing the user to drag-and-drop multiple images or upload a zip file of images. I could also allow the user to select multiple thumbnail sizes, add a watermark, generate a different image format, or any other image manipulation algorithms I wanted to offer for the resultant manipulated images. This could be very helpful for a user creating a catalog of products that need to be viewable on different devices where images need to be suitable for the particular presentation and not visually compromised by stretching or squashing a single version of the image.
Azure Account Setup and Web App Published
Before we get started developing the Azure Image Manipulator, you’ll need to create an Azure account. You can get a free one month trial of Azure at the Microsoft Azure sign up page. This takes only five minutes to accomplish and is demonstrated in the Sign up for Microsoft Azure video from Scott Hanselman. If you’ve already got a functional Azure account, even better…you saved 5 minutes.
Next, we want to get an ASP.NET MVC web app up and running in Azure. That might sound a bit intimidating if you haven’t done it yet, but it’s not. Once more, I’ll refer you to Mr. Hanselman for a quick 3 minute video entitled Create an ASP.NET Website using Visual Studio. In the video, he will walk you through spinning up a default ASP.NET MVC site all way to deploying your new web app to your Azure account.
I’ll assume at this point that you have a functional ASP.NET MVC web app running in Azure that we can gut and use for our own purposes.
Let’s See Some Code
The basic flow of our functionality is that we will accept the uploaded image from the user via a form POST on the HomeController. The HomeController will then package this into a class we are creating called UploadedImage. This is the model for everything we need to know about the image and the blueprint for the copies we want created. An instance of UploadedImage will later be inserted into the Azure Queue as our message to indicate that an image is ready for processing.
First, we’ll need to create a couple of model classes for UploadedImage and Thumbnail, like this:
Next, in the default HomeController, we’ll need to add an Upload action method to handle the posting of the file to the server. In the interest of keeping the Controller pretty lightweight, I’m going to create an ImageService to handle the image-centric logic. I have exposed this and I refer to it in my HomeController via an interface, IImageService. However, you’ll note that I’ve skipped the Dependency Injection for this simple example and just instantiated a concrete instance of ImageService in my HomeController. We’ll get to ImageService in just a bit. For now, just add this to your HomeController:
Now add the ImageService and IImageService. You’ll notice that for Url, I’ve just returned the incoming image as a base-64 encoded data url. Once we move to storing the image in Azure Blob Storage, I’ll replace that with the building of the actual url pointing to Azure Blob Storage.
Finally, you can replace the contents of your HomeIndex.cshtml with this:
At this point, you should be able to hit F5 and see the application run. You should be able to select an image and click the Upload button and you should see the image displayed in the page.
Publish and Wrap Up
Before I conclude this post, let’s go ahead and publish our updated web app to Azure. It’s as easy as right-clicking on your web project in the Solution Explorer and selecting “Publish..” and then clicking the Publish button. If you have any trouble, refer back to the second video I linked above from Scott Hanselman. He walks through the process in that video.
In Azure Bits #2 – Saving the Image to Azure Blob Storage, I walk you through setting up an Azure Blob Storage account and a Blob Storage Container in the Azure Portal and show how to use these in ImageService to save the original image to Azure Blob Storage and to view the Azure-published image in the browser.