【问题标题】:Download Images stored in Azure Blob Storage as Images using C#使用 C# 将存储在 Azure Blob 存储中的图像下载为图像
【发布时间】:2021-02-05 18:37:10
【问题描述】:

我在 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");
            }
        }

【问题讨论】:

    标签: azure


    【解决方案1】:

    您需要为您的图像打开一个读取流,并从该流中构造您的 .NET Image

    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 类还公开了一些其他有用的方法,例如下载到流。

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2018-08-03
      • 1970-01-01
      • 2016-10-11
      • 2019-04-03
      • 2020-10-08
      • 2018-07-11
      • 2019-12-06
      • 2020-10-04
      相关资源
      最近更新 更多