IronPDF 如何使用 使用 Azure Blob Storage 中的影像產生 PDF 使用 C# 渲染 Azure Blob 儲存影像的 PDF Curtis Chau 更新:7月 27, 2025 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English Azure Blob 儲存體是由微軟 Azure 提供的雲端為基礎的儲存服務。 它旨在儲存大量非結構化數據,例如文字或二進位數據,這些數據可以透過 HTTP 或 HTTPS 存取。 有些開發者希望使用儲存在 Azure Blob 儲存體中的映像。 這會帶來一個問題,因為圖像資料是以二進位形式儲存的,而不是以文件形式儲存的,而文件可以很容易地被 HTML 引用。 解決方法是將映像轉換為 base64 字串,然後將其新增到 img 標籤中。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronPDF PM > Install-Package IronPdf 複製並運行這段程式碼。 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"); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronPDF,免費試用! 免費試用30天 最小工作流程(5 個步驟) 下載 IronPDF 以渲染儲存在 Azure Blob 中的影像。 處理檢索 blob 的過程。 使用 ToBase64String 方法將位元組轉換為 Base64 字串。 在 img 標籤中包含 base64 訊息 將 HTML 渲染為 PDF 將 Azure Blob 轉換為 HTML 假設您已經設定了 Azure 儲存體帳戶並擁有包含 Blob 的容器,您還需要在 C# 專案中處理驗證和與 Azure 儲存體的連線。 之後,您可以使用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 ,然後可以使用ChromePdfRenderer的RenderHtmlAsPdf方法將其轉換為 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 Storage 中的影像渲染 PDF? 若要使用 Azure Blob Storage 中的圖片渲染 PDF,您可以使用 IronPDF 將圖片資料轉換為 base64 字串,將其嵌入 HTML img 標籤中,然後再使用 `RenderHtmlAsPdf` 方法將 HTML 渲染為 PDF。 將 Azure Blob Storage 中的影像轉換為 base64 字串需要哪些步驟? 首先,使用 `DownloadToStreamAsync` 方法下載圖片 Blob。然後,將影像串流轉換為位元組陣列,並使用 `Convert.ToBase64String` 將其轉換為 base64 字串。 如何在 HTML 文件中包含 Azure Blob Storage 的圖片? 從 Azure Blob Storage 以串流形式下載圖片,將其轉換為 base64 字串,並將其嵌入 HTML img 標籤中。這可讓您直接將圖片包含在 HTML 文件中。 Azure Blob Storage 在處理 PDF 的影像時扮演什麼角色? Azure Blob Storage 用於儲存非結構化的影像資料。將這些資料轉換為 base64 可讓其嵌入 HTML,方便將其納入使用 IronPDF 的 PDF 中。 如何用 C# 將內嵌圖片的 HTML 內容轉換成 PDF? 您可以使用 IronPDF 的 `RenderHtmlAsPdf` 方法將 HTML 內容(包括內嵌的 base64 圖片)轉換為 PDF。此方法處理 HTML 並將其渲染為 PDF 文件。 從 Azure Blob Storage 存取 Blob 需要驗證嗎? 是的,要在 C# 專案中建立與 Azure Storage 帳戶的連線,以安全地存取和管理 blobs,驗證是必要的。 將影像轉換為 base64 以進行 PDF 呈現的意義何在? 將影像轉換為 base64 可以將二進位影像資料直接以資料 URL 的形式嵌入 HTML 文件中,這些資料可以使用 IronPDF 準確地呈現成 PDF。 如何開始使用 IronPDF 渲染 PDF? 您可以從 NuGet 套件管理員下載 IronPDF。安裝完成後,您就可以使用其方法將 HTML 內容 (包括圖片) 呈現為 PDF 文件。 當從 Azure Blob Storage 渲染圖像時,IronPDF 是否與 .NET 10 應用程式完全相容? 是的,IronPDF 與 .NET 10 完全相容 - 支援 .NET 10 專案中的 HTML-to-PDF 渲染(包括圖片、CSS、JavaScript 等),例如網頁、桌面或雲端服務。您可以在 .NET 10 應用程式中使用相同的 Blob 下載 + base64 嵌入工作流程,而無需修改。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 16,493,056 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:16,493,056 檢視授權