在 mvc 中显示来自 azure 文件的图像列表

Display list of images from azure files in mvc

我有来自 azure 文件存储的图像文件列表,可以通过下面的代码

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference(ConfigurationManager.AppSettings["ShareReference"]);
if (share.Exists())
{
    CloudFileDirectory rootDir = share.GetRootDirectoryReference();
    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("freedom/" + DocName);
    if (sampleDir.Exists())
    {
        IEnumerable<IListFileItem> fileList = sampleDir.ListFilesAndDirectories();
                   //
    }

}

但是我无法找到一种方法来将它绑定到模型而不将其下载到项目中,以便我可以在我的视图中将 png 显示为缩略图。

这就是我获取文件列表中所有列表的方式

有多种方法可以达到同样的目的:

  • 处理此问题的最佳方法是创建实际图像的缩略图,同时将其存储到 Blob 存储。进一步在屏幕中,您可以将图像的缩略图下载到内存流并显示它。这将节省大量网络流量。

检查此 link 以获取更多参考:

https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/event-grid/resize-images-on-storage-blob-upload-event.md

或者,您可以编写自己的创建缩略图的代码,如下所示:

 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 (low quality and big size problem)

  • 另一种方法是将图像下载到内存流中并在控件中显示,但这会占用大量网络带宽。

参考:

https://www.c-sharpcorner.com/article/mvc-display-image-from-byte-array/

希望对您有所帮助。