VB.net code to read image blob and expose as base 64 string from WebAPI

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

Imports Microsoft.Azure
Imports Microsoft.WindowsAzure.Storage
Imports Microsoft.WindowsAzure.Storage.Blob
Imports System.IO

Namespace WebApplication1.Utilities
Public Class BlobStorageService

Public Function ReadImageContent(blobName As String) As String
Dim connectionString As String = CloudConfigurationManager.GetSetting("StorageConnectionString")

' Retrieve storage account from connection string.
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)

' Create the blob client.
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()

' Retrieve reference to a previously created container.
Dim container As CloudBlobContainer = blobClient.GetContainerReference("testblobaccountimportcontainer")

' Retrieve reference to a blob
Dim blockBlob2 As CloudBlockBlob = container.GetBlockBlobReference(blobName)

Dim datauri As String
Using memoryStream = New MemoryStream()
blockBlob2.DownloadToStream(memoryStream)
Dim bytes = memoryStream.ToArray()
Dim b64String = Convert.ToBase64String(bytes)
datauri = "data:image/png;base64," + b64String
End Using
Return datauri
End Function
End Class
End Namespace


Then write the below in a controller

Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Web.Http
Imports WebApplication1.Utilities

Namespace WebApplication1.Controllers
<RoutePrefix("BlobImage")> _
Public Class ValuesController
Inherits ApiController

<Route("BlobImageData")> _
<HttpGet> _
Public Function GetPhotoData() As String
Dim df As New BlobStorageService()
Dim blobImageContent = df.ReadImageContent("Capture.PNG")
Return blobImageContent
End Function
End Class
End Namespace

Comments or Responses

Login to post response