【问题标题】:Displaying an Image from Azure Storage before downloading - C#在下载之前显示 Azure 存储中的图像 - C#
【发布时间】:2018-12-10 11:47:33
【问题描述】:

我正在编写一个程序,允许用户从 Azure Blob 存储下载选定的图像。

我有它的工作,但是,目前,图像被下载到一个文件,然后这个文件路径用于显示图像。我希望显示图像,然后允许用户选择可以下载哪些图像。

下面是我下载图片的代码。

for (int i = 1; i<=dira.ListBlobs().Count(); i++)
{
     try
     {
          CloudBlob blob = dira.GetBlobReference(i + ".png");
          blob.DownloadToFile(localFilePath + "/" + i.ToString() + ".png", FileMode.Create);
          // MessageBox.Show(i.ToString());
      }
      catch (StorageException ex)
      {
      }
}

那么我的显示下载图片的代码就在这里:

pictureBox1.BackgroundImage= Image.FromFile(filePath + ".png");

在图片下载之前如何显示它们?

【问题讨论】:

  • 您可能需要上传两个版本的图像,一个完整分辨率版本和一个缩略图版本,然后在下载完整版本之前使用缩略图版本向您的用户显示图像。
  • CloudBLob 会给你图片的网址吗?
  • 如果不下载图像(或图像的一些拇指),您将无法显示图像
  • @Haytam 不是我能看到的。有没有办法不必将它们下载到文件中?因此,将它们下载到内存中。显示它们,然后如果需要图像,将它们下载到文件中?
  • 如果你有图片的网址,那么你可以使用PictureBox.Location,它将下载图片但在内存中。

标签: c# azure azure-storage picturebox


【解决方案1】:

如上所述,我们可以将它们下载到内存中。

下面是简单的代码供您参考:

 CloudBlob blob = dira.GetBlobReference(i + ".png");
 MemoryStream memoryStream = new MemoryStream();
 blob.DownloadToStream(memoryStream);
 pictureBox1.BackgroundImage = System.Drawing.Image.FromStream(memoryStream);

【讨论】:

    【解决方案2】:

    如果您想真正节省 PC 和 Blob 存储之间的一些网络流量(也就是下载时间),您只需在 Azure 中创建缩略图

    我发现了一个非常好的和完整的例子how to do that。 机制非常整洁且“多云”

    请记住,上述内容可能会增加您的 Azure 账单。与其他情况一样,您也需要考虑您的优先事项:

    • 我需要超级快速并为我的用户保存网络 -> 在 Azure 中创建缩略图

    • 我想节省成本,性能不是问题 -> 下载全尺寸图像并在主机上创建缩略图

    【讨论】:

      【解决方案3】:

      不下载就无法显示图片

      但是,

      您应该使用您的实际图像创建一个缩略图图像,这样当您向用户显示列表时,您可以从服务器下载缩略图,然后在用户选择时下载实际图像

      您可以使用以下代码创建缩略图

          public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
          { 
              //a holder for the result 
              Bitmap result = new Bitmap(width, height); 
      
              //use a graphics object to draw the resized image into the bitmap 
              using (Graphics graphics = Graphics.FromImage(result)) 
              { 
                  //set the resize quality modes to high quality 
                  graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
                  graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
                  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
                  //draw the image into the target bitmap 
                  graphics.DrawImage(image, 0, 0, result.Width, result.Height); 
              } 
      
              //return the resulting bitmap 
              return result; 
          } 
      

      参考:-C# Creating thumbnail

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        • 2018-11-21
        • 2018-08-03
        • 2013-12-18
        • 1970-01-01
        相关资源
        最近更新 更多