如何在 C# 中渲染存储在 Azure Blob 存储中的图像 | IronPDF

Rendering PDFs Containing Images Stored in Azure Blob Storage

This article was translated from English: Does it need improvement?
Translated
View the article in English

Azure Blob Storage 是由微软 Azure 提供的基于云的存储服务。 它旨在存储大量非结构化数据,如文本或二进制数据,可通过 HTTP 或 HTTPS 访问。

一些开发人员希望使用存储在 Azure Blob 存储中的图像。 这带来了一个问题,因为图像数据是以二进制形式存储的,而不是可以通过 HTML 轻松引用的文件。 解决方法是将图像转换为 base64 字符串,并将其添加到 img 标签中。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var blobBase64 = Convert.ToBase64String(new BlobContainerClient("conn","cont").GetBlobClient("img.jpg").DownloadContent().Value.Content.ToArray());
    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf($"<img src=\"data:image/jpeg;base64,{blobBase64}\" />").SaveAs("blobImage.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5个步骤)

  1. 下载 IronPDF 以渲染存储在 Azure Blob 中的图像
  2. 处理检索 blob 的过程
  3. 使用 ToBase64String 方法将字节转换为 base64
  4. 在 img 标签中包含 base64 信息
  5. 将 HTML 渲染为 PDF


将 Azure Blob 转换为 HTML

假设您已经设置了 Azure Storage 帐户并拥有一个包含 blob 的容器,您还需要在 C# 项目中处理对 Azure Storage 的身份验证和连接。 之后,您可以使用 DownloadToStreamAsync 方法将图像下载为流。 然后可以将流信息转换为 Base64 并嵌入到 HTML 的 img 标签中。 最后,可以将 imageTag 变量合并到 HTML 文档中。

using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public async Task ConvertBlobToHtmlAsync()
{
    // Define your connection string and container name
    string connectionString = "your_connection_string";
    string containerName = "your_container_name";

    // Initialize BlobServiceClient with the connection string
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

    // Get the BlobContainerClient for the specified container
    BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient(containerName);

    // Get the reference to the blob and initialize a stream
    BlobClient blobClient = blobContainer.GetBlobClient("867.jpg");
    using var stream = new MemoryStream();

    // Download the blob data to the stream
    await blobClient.DownloadToAsync(stream);
    stream.Position = 0; // Reset stream position

    // Convert the stream to a byte array
    byte[] array = stream.ToArray();

    // Convert bytes to base64
    var base64 = Convert.ToBase64String(array);

    // Create an img tag with the base64-encoded string
    var imageTag = $"<img src=\"data:image/jpeg;base64,{base64}\"/><br/>";

    // Use the imageTag in your HTML document as needed
}
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public async Task ConvertBlobToHtmlAsync()
{
    // Define your connection string and container name
    string connectionString = "your_connection_string";
    string containerName = "your_container_name";

    // Initialize BlobServiceClient with the connection string
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

    // Get the BlobContainerClient for the specified container
    BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient(containerName);

    // Get the reference to the blob and initialize a stream
    BlobClient blobClient = blobContainer.GetBlobClient("867.jpg");
    using var stream = new MemoryStream();

    // Download the blob data to the stream
    await blobClient.DownloadToAsync(stream);
    stream.Position = 0; // Reset stream position

    // Convert the stream to a byte array
    byte[] array = stream.ToArray();

    // Convert bytes to base64
    var base64 = Convert.ToBase64String(array);

    // Create an img tag with the base64-encoded string
    var imageTag = $"<img src=\"data:image/jpeg;base64,{base64}\"/><br/>";

    // Use the imageTag in your HTML document as needed
}
Imports Azure.Storage.Blobs
Imports System
Imports System.IO
Imports System.Threading.Tasks

Public Async Function ConvertBlobToHtmlAsync() As Task
	' Define your connection string and container name
	Dim connectionString As String = "your_connection_string"
	Dim containerName As String = "your_container_name"

	' Initialize BlobServiceClient with the connection string
	Dim blobServiceClient As New BlobServiceClient(connectionString)

	' Get the BlobContainerClient for the specified container
	Dim blobContainer As BlobContainerClient = blobServiceClient.GetBlobContainerClient(containerName)

	' Get the reference to the blob and initialize a stream
	Dim blobClient As BlobClient = blobContainer.GetBlobClient("867.jpg")
	Dim stream = New MemoryStream()

	' Download the blob data to the stream
	Await blobClient.DownloadToAsync(stream)
	stream.Position = 0 ' Reset stream position

	' Convert the stream to a byte array
	Dim array() As Byte = stream.ToArray()

	' Convert bytes to base64
	Dim base64 = Convert.ToBase64String(array)

	' Create an img tag with the base64-encoded string
	Dim imageTag = $"<img src=""data:image/jpeg;base64,{base64}""/><br/>"

	' Use the imageTag in your HTML document as needed
End Function
$vbLabelText   $csharpLabel

将HTML转换为PDF

imageTag 开始,它可以通过 ChromePdfRendererRenderHtmlAsPdf 方法转换为 PDF。

下面是如何调用 RenderHtmlAsPdf 的简单示例:

:path=/static-assets/pdf/content-code-examples/how-to/images-azure-blob-storage-html-to-pdf.cs
using IronPdf;

// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf(imageTag);

// Export to a file
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from a HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf(imageTag)

' Export to a file
pdf.SaveAs("imageToPdf.pdf")
$vbLabelText   $csharpLabel

确保调整 "htmlContent" 变量,以包含使用 imageTag 的实际 HTML 内容。

常见问题解答

如何渲染包含 Azure Blob 存储图像的 PDF?

要渲染包含 Azure Blob 存储图像的 PDF,可以使用 IronPDF 将图像数据转换为 base64 字符串,嵌入到 HTML img 标签中,然后使用 `RenderHtmlAsPdf` 方法将 HTML 渲染为 PDF。

转换 Azure Blob 存储中的图像为 base64 字符串需要哪些步骤?

首先,使用 `DownloadToStreamAsync` 方法下载图像 blob。然后将图像流转换为字节数组,并使用 `Convert.ToBase64String` 将其转换为 base64 字符串。

如何在 HTML 文档中包含来自 Azure Blob 存储的图像?

从 Azure Blob 存储中将图像下载为流,转换为 base64 字符串,并嵌入到 HTML 的 img 标签中。这让您可以直接在 HTML 文档中包含图像。

Azure Blob 存储在处理 PDF 图像方面有何作用?

Azure Blob 存储用于存储非结构化图像数据。将此数据转换为 base64 允许其嵌入到 HTML 中,便于使用 IronPDF 纳入 PDF。

如何在 C# 中将嵌入图像的 HTML 内容转换为 PDF?

您可以使用 IronPDF 的 `RenderHtmlAsPdf` 方法将包含嵌入 base64 图像的 HTML 内容转换为 PDF。此方法处理 HTML 并将其渲染为 PDF 文档。

我是否需要进行身份验证以从 Azure Blob 存储访问 Blob?

是的,需要身份验证来在 C# 项目中建立与 Azure 存储帐户的连接,以安全访问和管理 blob。

将图像转换为 base64 对于 PDF 渲染有何意义?

将图像转换为 base64 使得可以将二进制图像数据直接嵌入到 HTML 文档中作为数据 URL,可以用 IronPDF 准确地渲染为 PDF。

如何开始使用 IronPDF 渲染 PDF?

您可以从 NuGet 包管理器下载 IronPDF。安装后,您可以使用其方法将包括图像在内的 HTML 内容渲染为 PDF 文档。

IronPDF 在渲染来自 Azure Blob 存储的图像时是否与 .NET 10 应用程序完全兼容?

是的。IronPDF 完全兼容 .NET 10,支持在 .NET 10 项目(例如 Web、桌面或云服务)中进行 HTML 到 PDF 的渲染(包括图像、CSS、JavaScript 等)。您无需修改即可在 .NET 10 应用程序中使用相同的 Blob 下载 + base64 嵌入工作流程。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布