When uploading a Bitmap stream to Azure storage, it saves a null / blank image

I am using below code to load MemoryStream from bitmap to my azure account for Microsoft:

                         MemoryStream memoryStream = new MemoryStream();

                       img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

                       blob.Properties.ContentType = model.File.ContentType;
                       blob.UploadFromStream(memoryStream);

      

What happens when using the above code, it uploads an empty image to Azure storage :(! (I found the name, but the file size is zero!)!

       if (model.File != null && model.File.ContentLength > 0)
                {
                    Bitmap original = null;
                    var name = "newimagefile";
                    var errorField = string.Empty;

                    errorField = "File";
                    name = Path.GetFileNameWithoutExtension(model.File.FileName);
                    original = Bitmap.FromStream(model.File.InputStream) as Bitmap;
                   if (original != null)
                     {
                       var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);



                       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                                   CloudConfigurationManager.GetSetting("StorageConnectionString"));

                       CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                       CloudBlobContainer container = blobClient.GetContainerReference("company"); // must always be lowercase
                                                 container.CreateIfNotExists();

                       container.SetPermissions(
       new BlobContainerPermissions
       {
           PublicAccess =
               BlobContainerPublicAccessType.Blob
       });


                       CloudBlockBlob blob = container.GetBlockBlobReference(imgName + ".png");

                       MemoryStream memoryStream = new MemoryStream();

                       img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

                       blob.Properties.ContentType = model.File.ContentType;
                       blob.UploadFromStream(memoryStream);

      

}

Any help please!

+3


source to share


1 answer


I think you need to return the position to the beginning of the stream before loading



img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
memoryStream.Position = 0;
blob.UploadFromStream(memoryStream);

      

+8


source







All Articles