azure - 使用 C# 将存储在 Azure Blob 存储中的图像下载为图像
问题描述
我在 Azure Blob 存储中存储了一堆图像。现在我想检索它们并调整它们的大小。我已经成功地从帐户中读取了很多信息,例如文件名、上次修改日期和大小,但是如何获取实际图像?我看到的示例向我展示了如何将其下载到文件中,但这对我没有用,我想将其作为图像下载,以便进行处理。
这是我到目前为止所拥有的:
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Console.WriteLine("Listing blobs...");
// build table to hold the info
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("blobItemName", typeof(string));
table.Columns.Add("blobItemLastModified", typeof(DateTime));
table.Columns.Add("blobItemSizeKB", typeof(double));
table.Columns.Add("blobImage", typeof(Image));
// row counter for table
int intRowNo = 0;
// divider to convert Bytes to KB
double dblBytesToKB = 1024.00;
// List all blobs in the container
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
// increment row number
intRowNo++;
//Console.WriteLine("\t" + blobItem.Name);
// length in bytes
long? longContentLength = blobItem.Properties.ContentLength;
double dblKb = 0;
if (longContentLength.HasValue == true)
{
long longContentLengthValue = longContentLength.Value;
// convert to double DataType
double dblContentLength = Convert.ToDouble(longContentLengthValue);
// Convert to KB
dblKb = dblContentLength / dblBytesToKB;
}
// get the image
// **** Image thisImage = what goes here ?? actual data from blobItem ****
// Last modified date
string date = blobItem.Properties.LastModified.ToString();
try
{
DateTime dateTime = DateTime.Parse(date);
//Console.WriteLine("The specified date is valid: " + dateTime);
table.Rows.Add(intRowNo, blobItem.Name, dateTime, dblKb);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse the specified date");
}
}
解决方案
您需要为您的图像打开一个读取流,并Image
从该流构建您的 .NET:
await foreach (BlobItem item in containerClient.GetBlobsAsync()){
var blobClient = containerClient.GetBlobClient(item.Name);
using Stream stream = await blobClient.OpenReadAsync();
Image myImage = Image.FromStream(stream);
//...
}
blobclient类还公开了一些其他有用的方法,例如下载到流。
推荐阅读
- javascript - 单击复选框检查值
- python-3.x - 获取成员列表的 Python 错误
- sql - 循环遍历结果集
- ios - 展开可选值
- angular - Angular完全销毁动态创建的组件
- ios - IOS/Objective-C:在 Objective-C 中可能没有处理程序的 NSTimer?
- python - 将 Django 连接到 Postgres:django.db.utils.OperationalError:致命:数据库“DATABASENAME”不存在
- c - 如何二进制扇出
- c# - 检查字符串是否包含任何字符串数组值,然后获取子字符串
- ios - iOS 如何通过 Firebase 分析解决“您已经购买了此商品是否还想再次购买”的问题