Azure Blob storage is a service for storing large amounts of unstructured data. In this article we will look how we can save a Image File from Blob Container.
Introduction
Azure Blob storage is a service for storing large amounts of unstructured data. In this article we will look how we can save a Image File from Blob Container.
Step 1: Create a Source Blob Container in the Azure Portal
Open the Azure portal(https://portal.azure.com), and then choose Storage Account say Containers
Create a source container say sourcecontainer
It will appear as
Step 2: Upload a file in the Source Container
Let us upload an image file in sourcecontainer.
Step 3: Save a Image File from Blob Container programatically
Fire up a console application and add the below Nuget packages
Install-Package WindowsAzure.Storage
Install-Package Microsoft.WindowsAzure.ConfigurationManager
Next write the below function
/// <summary>
/// SaveBlobImage
/// Saves the blob image in local drive
/// </summary>
/// <param name="filename"></param>
/// <param name="connectionString"></param>
/// <param name="containerName"></param>
/// <returns></returns>
private static void SaveBlobImage(string filename, string connectionString, string containerName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Retrieve reference to a blob named "1.jpg"
CloudBlockBlob blockBlobReference = container.GetBlockBlobReference(filename);
using (var memoryStream = new MemoryStream())
{
//downloads blob's content to a stream
blockBlobReference.DownloadToStream(memoryStream);
//creates an image from the memory stream
using (Image image = Image.FromStream(memoryStream))
{
//save the image
image.Save("D:\\blobImage.jpg", ImageFormat.Jpeg);
}
}
}
The above function gets the blob's reference and using the DownloadToStream function, it downloads the blob's content to a memory stream. This stream of data is then converted to an image by the Image.FromStream function and it is saved in the local drive.
Now from inside the main function, let us write the below code
static void Main(string[] args)
{
string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString"); //blob connection string
string sourceContainerName = ConfigurationManager.AppSettings["sourcecontainerName"]; //source blob container name
string sourceBlobFileName = "1.jpg"; //source blob name
SaveBlobImage(sourceBlobFileName, connectionString, sourceContainerName);
}
The program invokes the SaveBlobImage function to save the blob content as image.

The app.config file looks as under
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=testaccount;AccountKey=CRd****==;EndpointSuffix=core.windows.net" />
<add key="sourcecontainerName" value="sourcecontainer" />
</appSettings>
The complete program is as under
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace UploadFilesToBlob
{
class Program
{
static void Main(string[] args)
{
string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString"); //blob connection string
string sourceContainerName = ConfigurationManager.AppSettings["sourcecontainerName"]; //source blob container name
string sourceBlobFileName = "1.jpg"; //source blob name
SaveBlobImage(sourceBlobFileName, connectionString, sourceContainerName);
}
private static void SaveBlobImage(string filename, string connectionString, string containerName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Retrieve reference to a blob named "1.jpg"
CloudBlockBlob blockBlobReference = container.GetBlockBlobReference(filename);
using (var memoryStream = new MemoryStream())
{
//downloads blob's content to a stream
blockBlobReference.DownloadToStream(memoryStream);
//creates an image from the memory stream
using (Image image = Image.FromStream(memoryStream))
{
//save the image
image.Save("D:\\blobImage.jpg", ImageFormat.Jpeg);
}
}
}
}
}
Conclusion
In this article we looked into how save an Image File from Blob Container. Hope this will be helpful. Thanks for reading. Zipped file attached.