Thursday 5 January 2017

How to post image in blob using Windows Azure.

First Set Connection String in Web Config File
============================
 <!--Blob Connnection-->
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=satyastorageaccount;AccountKey=SrVl+wKtyrty64564564646464545vPeFthtrhrt7vQXdxjr1Fp6e67tutut==" />
    <!--End BlobConnection-->
=================================

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Telerik.Web.UI.ImageEditor;


namespace testassss.Admin
{
    public partial class CompanyBranding : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public static Bitmap ResizeImage(Stream stream, int hght, int wdth)
        {
            int height = hght;//150;
            int width = wdth; //150;
            Bitmap scaledImage = new Bitmap(width, height);
            try
            {
                System.Drawing.Image originalImage = Bitmap.FromStream(stream);
                using (Graphics g = Graphics.FromImage(scaledImage))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(originalImage, 0, 0, width, height);
                    return scaledImage;
                }
            }
            catch (Exception Exc)
            {
                Exc.ToString();
                return scaledImage;
            }
        }
        public static Bitmap ResizeImage1(Stream stream, int hghtw, int wdthw)
        {
            int height = hghtw;//150;
            int width = wdthw; //150;
            Bitmap scaledImage1 = new Bitmap(width, height);
            try
            {
                System.Drawing.Image originalImage = Bitmap.FromStream(stream);
                using (Graphics h = Graphics.FromImage(scaledImage1))
                {
                    h.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    h.DrawImage(originalImage, 0, 0, width, height);
                    return scaledImage1;
                }
            }
            catch (Exception Exc)
            {
                Exc.ToString();
                return scaledImage1;
            }
        }
        protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            try
            {
                Bitmap bitmapImage = ResizeImage(RadAsyncUpload1.UploadedFiles[0].InputStream, 120, 130);
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                RadBinaryImage1.DataValue = stream.ToArray();
                ViewState["Signbin"] = stream.ToArray();
                Session["FileSize"] = e.File.ContentLength;
                Session["FileType"] = e.File.ContentType;
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Error Message:')</script>" + ex.Message);
            }
        }
        protected void RadAsyncUpload2_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            try
            {
             
                Bitmap bitmapImage = ResizeImage1(RadAsyncUpload2.UploadedFiles[0].InputStream, 120, 130);
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);          
                RadBinaryImage2.DataValue = stream.ToArray();
                //RadBinaryImage3.DataValue = stream.ToArray();
                //RadBinaryImage4.DataValue = stream.ToArray();
                //RadBinaryImage5.DataValue = stream.ToArray();          
                //RadBinaryImage2.DataValue = stream.ToArray();
                ViewState["Signbin"] = stream.ToArray();
                Session["FileSize"] = e.File.ContentLength;
                Session["FileType"] = e.File.ContentType;
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Error Message:')</script>" + ex.Message);
            }
        }
        public static CloudStorageAccount GetConnectionString()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            return storageAccount;
        }
        public async Task<string> UploadImageAsync(UploadedFile imageToUpload)
        {
            string floder = RadTextBox2.Text;
            string imageFullPath = null;
            if (imageToUpload == null || imageToUpload.ContentLength == 0)
            {
                return null;
            }
            try
            {
                CloudStorageAccount cloudStorageAccount = GetConnectionString();
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(floder);
                CloudBlobDirectory folder = cloudBlobContainer.GetDirectoryReference(floder + "image");
                cloudBlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
                string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);
                CloudBlockBlob cloudBlockBlob = folder.GetBlockBlobReference(imageName);
                cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
                cloudBlockBlob.UploadFromStream(imageToUpload.InputStream);
                imageFullPath = cloudBlockBlob.Uri.ToString();
                ViewState["Image"] = imageFullPath;
            }
            catch (Exception ex)
            {

            }
            return imageFullPath;
        }
        public async Task<string> UploadImageAsync1(UploadedFile imageToUpload)
        {
            string floder = RadTextBox2.Text;
            string imageFullPath = null;
            if (imageToUpload == null || imageToUpload.ContentLength == 0)
            {
                return null;
            }
            try
            {
                CloudStorageAccount cloudStorageAccount = GetConnectionString();
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(floder);
                CloudBlobDirectory folder = cloudBlobContainer.GetDirectoryReference(floder +"image");
                cloudBlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
                string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);
                CloudBlockBlob cloudBlockBlob = folder.GetBlockBlobReference(imageName);
                cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
                cloudBlockBlob.UploadFromStream(imageToUpload.InputStream);
                imageFullPath = cloudBlockBlob.Uri.ToString();
               ViewState["Image1"] = imageFullPath;
            }
            catch (Exception ex)
            {

            }
            return imageFullPath;
        }
        protected void RadButton1_Click(object sender, EventArgs e)
        {
             try
             {
                 string floder = RadTextBox2.Text;          
             
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("swasherpstorageaccount", "ffwfweFQ2BviaUvQXdxjryrty4546rgrtzvw=="), true);
                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                // Retrieve a reference to a container.
                CloudBlobContainer container = blobClient.GetContainerReference(floder);
                // Retrieve reference to a blob named "myblob".
                //CloudBlockBlob blockBlob = container.GetBlockBlobReference("swash");
                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                var img = UploadImageAsync(RadAsyncUpload1.UploadedFiles[0]);
                var img1 = UploadImageAsync1(RadAsyncUpload2.UploadedFiles[0]);
                ErpDataAccess.GetInstance.InsertData(ViewState["Image"].ToString());
                // myph.InsertData(ViewState["Image"].ToString());
                ViewState["Image"] = null;              
            }
            catch (Exception Exc)
            {
            }          
        }
    }
}

No comments:

Post a Comment