If using ImageResizer with Azure blobs do I need the AzureReader2 plugin?

azure, azure-blob-storage, imageresizer

Solution

With some trepidation, I'm going to disagree with astaykov here. I believe you CAN use ImageResizer with Azure WITHOUT needing AzureReader2. Maybe I should qualify that by saying 'It works on my setup' :)

I'm using ImageResizer in an MVC 3 application. I have a standard Azure account with an images container.

Here's my test code for the view:

@using (Html.BeginForm( "UploadPhoto", "BasicProfile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

And here's the corresponding code in the Post Action method:

// This action handles the form POST and the upload
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0)
    {
        string newGuid = Guid.NewGuid().ToString();

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

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

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

        // Retrieve reference to the blob we want to create            
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(newGuid + ".jpg");

        // Populate our blob with contents from the uploaded file.
        using (var ms = new MemoryStream())
        {
            ImageResizer.ImageJob i = new ImageResizer.ImageJob(file.InputStream,
                    ms, new ImageResizer.ResizeSettings("width=800;height=600;format=jpg;mode=max"));
            i.Build();

            blockBlob.Properties.ContentType = "image/jpeg";
            ms.Seek(0, SeekOrigin.Begin);
            blockBlob.UploadFromStream(ms);
        }
    }

    // redirect back to the index action to show the form once again
    return RedirectToAction("UploadPhoto");
}

This is 'rough and ready' code to test the theory and could certainly stand improvement but, it does work both locally and when deployed on Azure. I can also view the images I've uploaded, which are correctly re-sized.

Hope this helps someone.

Problem

I'm working on a personal project to manage users of my club, it's hosted on the free Azure package (for now at least), partly as an experiment to try out Azure. Part of creating their records is to add a photo, so I've got a Contact Card view that lets me see who they are, when they came and a photo. I have installed ImageResizer and it's really easy to resize the 10MP photos from my camera and save them to the file system locally, but it seems that for Azure I need to use their `Blobs` to Upload Pictures to Windows Azure Web Sites, and that's new to me. The documentation on ImageResizer says that I need to use `AzureReader2` in order to work with Azure blobs but it isn't free. It also says in their best practices #5 to Use dynamic resizing instead of pre-resizing your images. Which is not what I was thinking, I was going to resize to 300x300 and 75x75 (for thumbnail) when creating the users record. But if I should be storing full size images as blobs and dynamically resizing on the way out then can I just use standard means to Upload a blob into a container to save it to Azure, then when I want to display the images use the ImageResizer and pass it each image to resize as required. That way not needing to use the AzureReader2, or have I misunderstood what it does / how it works? Is there another way to consider? I've not yet implemented cropping, but that's next to tackle when I've worked out how to actually store the images properly

Original source

Related problems