图像未正确上传到 Azure 存储
Image not getting uploaded properly to Azure Storage
我正在尝试将图像上传到 Azure 存储。图片确实上传到给定路径,但上传的图片似乎已上传 incompletely/cropped。我在不同的路径上传不同尺寸的图片,但它们都遇到了同样的问题。
我尝试将这些图像保存在本地文件系统上,它们保存得很好。
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;
}
这是正在上传的图片与原始图片的对比示例。
只有部分图片可见,其余部分不可见
正如 Manan Kapoor and Gaurav Mantri 之间讨论的那样,添加要点作为答案以帮助可能面临类似问题的其他社区成员。
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();
}
}
我正在尝试将图像上传到 Azure 存储。图片确实上传到给定路径,但上传的图片似乎已上传 incompletely/cropped。我在不同的路径上传不同尺寸的图片,但它们都遇到了同样的问题。
我尝试将这些图像保存在本地文件系统上,它们保存得很好。
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;
}
这是正在上传的图片与原始图片的对比示例。
只有部分图片可见,其余部分不可见
正如 Manan Kapoor and Gaurav Mantri 之间讨论的那样,添加要点作为答案以帮助可能面临类似问题的其他社区成员。
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();
}
}