c# - Azure Blob TIFF 到 PNG 转换,无需下载

标签 c# asp.net-mvc azure

我正在尝试找出一种在 img 标记中显示 tiff 图像(从 Azure blob 存储中提取)的方法,而无需使用 tiff 查看器。我的第一个想法是将图像从 tiff 转换为 png,然后在浏览器中将其显示为 base64 编码图像。

我见过经常使用 Image.Save 方法,但我似乎不知道如何在不将图像保存到磁盘的情况下使用它。

有没有办法可以将 tiff 即时转换为 png,以便它可以在浏览器中呈现?

工作代码

公共(public) ActionResult Index()

{
    CloudBlobContainer blobContainer = GetContainer(documentsContainerName);
    IEnumerable<string> blobs = blobContainer.ListBlobs().Select(blob =>
    {
        CloudBlockBlob blockBlob = (CloudBlockBlob)blob;

        using (MemoryStream blockBlobStream = new MemoryStream())
        {
            blockBlob.DownloadToStream(blockBlobStream);

            using (MemoryStream ms = new MemoryStream())
            {
                using (Image img = Image.FromStream(blockBlobStream))
                {
                    img.Save(ms, ImageFormat.Png);
                }

                StreamReader reader = new StreamReader(ms);

                byte[] bytedata = System.Text.Encoding.Default.GetBytes(reader.ReadToEnd());

                string strBase64 = System.Convert.ToBase64String(bytedata);

                return strBase64;
            }
        }
    });

    return View("Index", "", blobs);
}

最佳答案

根据@Dai的建议,我能够使用 Magick.Net 将我的blob转换为png nuget 包。

using (MemoryStream memStream = new MemoryStream())
{
    using (MagickImage image = new MagickImage(byteArray))
    {
        image.Format = MagickFormat.Png;
        image.Write(memStream);
    }
    return System.Convert.ToBase64String(memStream.ToArray());
}

关于c# - Azure Blob TIFF 到 PNG 转换,无需下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767612/

相关文章:

c# - 如何在 MVC 4 中显示来自路径的图像?

javascript - 外部 js 文件在 vs 2013 中未更新

c# - 如何使用 RedirectToAction 返回方法参数

azure - 使用 Azure Databricks 中的 Snowpark - 此群集上未启用 Unity Catalog

azure - 使用 terraform 如何将自定义域添加到 Azure 前门

visual-studio - Visual Studio 2013 和部署 Typescript Html 模板

c# - 使用(DataContext)还是不使用

javascript - 对 web 方法的 Ajax 调用给出错误 500 内部服务器错误

asp.net-mvc - 保持客户端和服务器同步的单页应用程序

C# MVC 如何将按钮值传回 Controller ?