.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Azure >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating Picture Gallery Using Windows Azure Blob Storage

Posted By:Sunil Yadav       Posted Date: August 07, 2010    Points: 50    Category: Azure    URL: http://www.dotnetspark.com  

This article explains how we can take advantage of Windows Azure Blob storage to create a picture gallery.
 

In this article I am going to explain how we can take advantage of Windows Azure Blob storage to create a picture gallery.

Windows Azure Blob Storage?

Blob Storage is one of the components of Windows Azure Storage services for storing text or binary data like images, videos etc.

There are two types of Blob Storage listed below.

1.        It is optimized for streaming. Each blob consists of a sequence of blocks identified by a Block ID. The Size limit is 200GB per blob.

 

2.        It is optimized for random read/write operation. It is identified by its offset from the start of the blob.

 

For more info: http://msdn.microsoft.com/en-us/library/ee691964.aspx

A URL that refers to a blob contains a container and account name as shown below.

Description: C:\Users\sunil\Desktop\blob diagram.png

 

Creating solution using visual studio 2008.

Step 1:  Create a new cloud project and select Windows Azure Cloud Service from installed template using c# as shown below.

Description: C:\Users\sunil\Desktop\demo\1 create new cloud project.png

 

Step 2: Add ASP.Net Web Role from roles list and name it BlobDemo.

Description: C:\Users\sunil\Desktop\demo\2 add worker role.png

Step 3: Add new item and select Generic Handler Since we are using a generic HTTP Handler to display the images in a different size.

Description: C:\Users\sunil\Desktop\demo\3 add generic handler.png

 

Step 5: Once done with creating a project, the solution explorer looks like below

Description: D:\Demos\Blog Storage Demo\4 solution explorer.png

 

Step 6:  Below is the Service Configuration (ServiceConfiguration.cscfg) file which stores the application configuration shown below.

 

. Description: C:\Users\sunil\Desktop\demo\6. Default storage account.png

Step 7: Press F5 to run the application.

Description: C:\Users\sunil\Desktop\demo\5 Files uploaded demo.png

Code:
Code for ImageHandler.ashx

<%@ WebHandler Language="C#" Class="ImageHandler" Debug="true" %>

using System;
using System.Web;
using System.IO;
using System.Drawing

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string sImageFileName = "";
        int iThumbSize = 0;
        decimal dHeight, dWidth, dNewHeight, dNewWidth;
        bool bSz = HttpContext.Current.Request.QueryString["sz"] != null ? HttpContext.Current.Request.QueryString["sz"] != string.Empty ? true : false : false;
        bool bImg = HttpContext.Current.Request.QueryString["img"] != null ? HttpContext.Current.Request.QueryString["img"] != string.Empty ? true : false : false;
        if (bImg && bSz)
        {
            sImageFileName = context.Request.QueryString["img"];
            iThumbSize = Convert.ToInt32(context.Request.QueryString["sz"]);

            //check for diffrent file extension and filformat case like .jpg-->.JPG etc.
            System.Drawing.Image objImage = null;
            try
            {
                objImage = Bitmap.FromStream(System.Net.HttpWebRequest.Create(sImageFileName).GetResponse().GetResponseStream());
            }
            catch (FileNotFoundException)
            {


            }
            if (sImageFileName != null)
            {
                if (iThumbSize == 1)
                {

                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 80;
                    dNewWidth = dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }
                if (iThumbSize == 2)
                {
                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 300;
                    dNewWidth = dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }
                if (iThumbSize == 3)
                {
                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 400;
                    dNewWidth = dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dWidth, (int)dHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }
                if (iThumbSize == 4)
                {

                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 100;
                    dNewWidth = 100;// dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }

                if (iThumbSize == 5)
                {

                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 40;
                    dNewWidth = 40;// dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }
                if (iThumbSize == 6)
                {

                    dHeight = objImage.Height;
                    dWidth = objImage.Width;
                    dNewHeight = 400;
                    dNewWidth = 500;// dWidth * (dNewHeight / dHeight);
                    objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new Image.GetThumbnailImageAbort(callback), new IntPtr());
                }
                MemoryStream objMemoryStream = new MemoryStream();
                //Check For Format 
                objImage.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageContent = new byte[objMemoryStream.Length];
                objMemoryStream.Position = 0;
                objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(imageContent);
            }
        }

    }
    private bool callback()
    {
        return true;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
Code for Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BlobDemo._Default" %>




    


    
    

Blob Stroage Demo

Enter directory path :  

Defailt.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;

namespace BlobDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        private CloudBlobClient blobClient;
        private CloudBlobContainer blobContainer;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (txtDirectory.Text != "")
            {
                var blobStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;// Use DevelopmentStorageAccount only in the development environment
                blobClient = blobStorageAccount.CreateCloudBlobClient();

                blobContainer = blobClient.GetContainerReference("imagecontainer");
                blobContainer.CreateIfNotExist();

                BlobContainerPermissions blobPermission = new BlobContainerPermissions();
                blobPermission.PublicAccess = BlobContainerPublicAccessType.Container;
                blobContainer.SetPermissions(blobPermission);

                try
                {
                    DirectoryInfo directory = new DirectoryInfo(txtDirectory.Text.Trim());
                    FileInfo[] files = directory.GetFiles("*.jpg"); // You can also provide filter for .png,.jpeg files.

                    foreach (FileInfo file in files)
                    {
                        FileStream fs = File.OpenRead(file.FullName);
                        var blob = blobContainer.GetBlobReference(file.Name);
                        blob.UploadFromStream(fs);

                        blob.Metadata["FileName"] = file.Name;
                        blob.Metadata["FileExtension"] = file.Extension;
                        blob.SetMetadata();

                        blob.Properties.ContentType = "image/jpeg";
                        blob.SetProperties();
                    }
                    lblmsg.Text = "File(s) uploaded successfully.";
                }
                catch (Exception ex)
                {

                    throw;
                }
            }
        }
        protected void btnRetrieve_Click(object sender, EventArgs e)
        {
            dtlImages.DataSource = GetPictures();
            dtlImages.DataBind();

        }
        private List GetPictures()
        {
            var storageAccount = CloudStorageAccount.DevelopmentStorageAccount; // Use DevelopmentStorageAccount only in the development environment
            blobClient = storageAccount.CreateCloudBlobClient();
            List uri = new List();

            foreach (var properties in blobClient.GetContainerReference("imagecontainer").ListBlobs())
            {
                uri.Add(properties.Uri);
            }
            return uri;
        }

    }
}

References:

http://msdn.microsoft.com/en-us/library/dd179376.aspx

Happy Coding.

Download Project: http://www.sunilyadav.net/wp-content/uploads/BlobStorageDemo.zip 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend