C# code to read image blob and expose as base 64 string from WebAPI

Posted by Rajnilari2015 under Azure category on | Points: 40 | Views : 11016
Let us first write a utility service as under

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;

namespace WebApplication1.Utilities
public class BlobStorageService

public string ReadImageContent(string blobName)
string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

// 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("testblobaccountimportcontainer");

// Retrieve reference to a blob
CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(blobName);

string datauri;
using (var memoryStream = new MemoryStream())
var bytes = memoryStream.ToArray();
var b64String = Convert.ToBase64String(bytes);
datauri = "data:image/png;base64," + b64String;
return datauri;

Then write the below in a controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Utilities;

namespace WebApplication1.Controllers
public class ValuesController : ApiController

public string GetPhotoData()
BlobStorageService df = new BlobStorageService();
var blobImageContent = df.ReadImageContent("Capture.PNG");
return blobImageContent;

Comments or Responses

Login to post response