下载之前从Azure存储显示图像-C#

时间:2018-07-02 09:29:43

标签: c# azure azure-storage picturebox

我正在编写一个程序,该程序将允许用户从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");

在下载图像之前如何显示图像?

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

我找到了一个非常漂亮且完整的示例how to do that。 机制非常整洁且“多云”

Thumbnail in cloud

请记住,以上可能会增加您的Azure费用。像在其他情况下一样,您也需要考虑您的优先事项:

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

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

答案 2 :(得分:0)

  

如果不下载图片就无法显示图片

但是,

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

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

    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

相关问题