Here is my code:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Threading.Tasks;
using Navistar.Inventory.Business.Domain.Interfaces;
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
using System.Drawing.Drawing2D;
//get the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse([ConnectionString]);
//instantiate the client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//set the container
CloudBlobContainer container = blobClient.GetContainerReference([ImagesContainerName]);
var blobUrl = Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference([blobUrl]);
using (var client = new WebClient())
{
using (var stream = await client.OpenReadTaskAsync([feedUrl]))
{
if (stream != null)
{
//resize large image
Image img = Image.FromStream(stream);
img = Resize(img);
//save to stream with content type
if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/jpeg";
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/png";
}
else if (ImageFormat.Bmp.Equals(img.RawFormat))
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/bmp";
}
else if (ImageFormat.MemoryBmp.Equals(img.RawFormat))
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/bmp";
}
//upload
await blockBlob.UploadFromStreamAsync(stream);
}
}
client.Dispose();
}
public Image Resize(Image image)
{
var destRect = new Rectangle(0, 0, int.Parse(_config.LargeImageWidth), int.Parse(_config.LargeImageHeight));
var destImage = new Bitmap(int.Parse(_config.LargeImageWidth), int.Parse(_config.LargeImageHeight));
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
As you can see, I'm uploading images to Azure Storage. If I upload an image without resizing it, the image appears when navigated to through Azure Portal. If I resize the image before uploading it, the image is blank.
The image from the feedUrl (the Stream) is in Jpeg format. Once it is re-sized, it is in MemoryBmp format.
According to your description, the reason why the upload image size is 0 byte is the stream has the position value and you don't set the position value to 0 when you upload the steam to the blob storage.
Besides, since the clientstream has already has the value, but you still save the resized image stream to the clientstream, that means you store two image into one image.
I suggest you could create a new memory stream which stored the resized image data.
More details, you could refer to below code sample:
Main method:
//get the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
//instantiate the client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//set the container
CloudBlobContainer container = blobClient.GetContainerReference("brando");
var blobUrl = Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("4.PNG");
using (var client = new WebClient())
{
using (var stream1 = client.OpenRead("url"))
{
//used to store the resized image stream
MemoryStream m2 = new MemoryStream();
if (stream1 != null)
{
//resize large image
Image img = Image.FromStream(stream1);
img = Resize(img);
//save to stream with content type
if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
img.Save(m2, System.Drawing.Imaging.ImageFormat.Jpeg);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/jpeg";
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
img.Save(m2, System.Drawing.Imaging.ImageFormat.Png);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/png";
}
else if (ImageFormat.Bmp.Equals(img.RawFormat))
{
img.Save(m2, System.Drawing.Imaging.ImageFormat.Bmp);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/bmp";
}
else if (ImageFormat.MemoryBmp.Equals(img.RawFormat))
{
img.Save(m2, System.Drawing.Imaging.ImageFormat.Bmp);
//imageBytes = ms2.ToArray();
blockBlob.Properties.ContentType = "image/bmp";
}
m2.Position = 0;
//upload
blockBlob.UploadFromStream(m2);
}
}
client.Dispose();
Resize method:
public static Image Resize(Image image)
{
var destRect = new Rectangle(0, 0, int.Parse("1024"), int.Parse("1024"));
var destImage = new Bitmap(int.Parse("1024"), int.Parse("1024"));
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
Result: