I am trying to upload my image to Azure Storage. The image does get uploaded on the given path but the uploaded image seems to be uploaded incompletely/cropped. I am uploading different sizes of images on different paths and all of them are having the same issue.
I tried saving these images on local file system and they are saving perfectly.
public static void saveImage(string fileName, Stream fileContent, string contentType)
{
fileContent.Seek(0, SeekOrigin.Begin);
CloudBlockBlob blob = null;
blob = _blobImageContainer.GetBlockBlobReference(fileName);
byte[] bytes = getByteArray(fileContent);
bool isCompressableImage = isCompressable(fileName);
if (isCompressableImage)
{
bytes = getCompressedArray(bytes);
}
blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length).Wait();
blob.Properties.ContentType = contentType;
if (isCompressableImage)
{
blob.Properties.ContentEncoding = "gzip";
}
blob.SetPropertiesAsync().Wait();
}
private static byte[] getByteArray(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
private static byte[] getCompressedArray(byte[] bytes)
{
using (MemoryStream comp = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(comp, CompressionLevel.Optimal))
{
gzip.Write(bytes, 0, bytes.Length);
}
bytes = comp.ToArray();
}
return bytes;
}
Here is a sample of the image being uploaded vs the original image.
Only some part of the image can be seen, rest part is not visible.
As discussed between Manan Kapoor and Gaurav Mantri, adding gist as an answer to help other community members who might face similar issue.
The problem was in the way the
fileContent
stream was being generated.
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(receiveStream))
{
b = br.ReadBytes(500000);
br.Close();
}
}