IronPDF 操作指南 使用 Azure Blob 存儲中的圖片生成 PDF 在 C# 中使用 Azure Blob Storage 影像渲染 PDF Curtis Chau 更新:2026年1月10日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 若要在 C# 中使用 Azure Blob Storage 圖像渲染 PDF,請以二進位方式擷取 Blob 資料,將其轉換為 base64 字串,嵌入 HTML img 標籤中,並使用 IronPDF 的 ChromePdfRenderer 將 HTML 轉換為 PDF。 Azure Blob 儲存體是由微軟 Azure 提供的雲端為基礎的儲存服務。 它儲存大量非結構化資料,例如文字或二進位資料,可透過 HTTP 或 HTTPS 存取。 在 C# 中處理 PDF 時,IronPDF 提供了強大的功能,可處理各種影像格式和來源,包括儲存在 Azure Blob Storage 等雲端服務中的影像。 若要使用 Azure Blob Storage 中儲存的影像,您必須處理二進位資料格式,而非直接的檔案參照。 解決方案是將影像轉換為 base64 字串,並將其嵌入 img 標籤中。 此方法可與 IronPDF 的 HTML 至 PDF 轉換功能無縫配合,維持影像品質與格式。 快速入門:使用 Azure Blob 儲存影像渲染 PDF 立即開始使用 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? <! -- --> <!--說明:顯示程式碼執行輸出或結果的截圖 --> 使用包含 blobs 的容器設定 Azure Storage 帳戶,然後在 C# 專案中處理驗證與連線。 在使用 IronPDF 的同時使用 Azure.Storage.Blobs NuGet 套件。 對於複雜的認證情境,請探索 IronPDF 的 HTTP 請求標頭功能,以進行安全的 blob 存取。 使用 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 在處理多種圖片或不同格式的圖片時,請利用 IronPDF 對各種圖片類型的支援,包括 JPG、PNG、SVG 和 GIF。 base64 編碼方法適用於所有這些格式。 使用不同的圖像格式 Azure Blob Storage 支援多種影像格式,IronPDF 在正確編碼後可處理所有影像格式。 以下是動態判斷 MIME 類型的增強範例: public string GetImageMimeType(string blobName) { var extension = Path.GetExtension(blobName).ToLower(); return extension switch { ".jpg" or ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".svg" => "image/svg+xml", ".webp" => "image/webp", _ => "image/jpeg" // default fallback }; } public async Task<string> CreateImageTagFromBlob(BlobClient blobClient) { using var stream = new MemoryStream(); await blobClient.DownloadToAsync(stream); stream.Position = 0; var base64 = Convert.ToBase64String(stream.ToArray()); var mimeType = GetImageMimeType(blobClient.Name); return $"<img src=\"data:{mimeType};base64,{base64}\" alt=\"{Path.GetFileNameWithoutExtension(blobClient.Name)}\"/>"; } public string GetImageMimeType(string blobName) { var extension = Path.GetExtension(blobName).ToLower(); return extension switch { ".jpg" or ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".svg" => "image/svg+xml", ".webp" => "image/webp", _ => "image/jpeg" // default fallback }; } public async Task<string> CreateImageTagFromBlob(BlobClient blobClient) { using var stream = new MemoryStream(); await blobClient.DownloadToAsync(stream); stream.Position = 0; var base64 = Convert.ToBase64String(stream.ToArray()); var mimeType = GetImageMimeType(blobClient.Name); return $"<img src=\"data:{mimeType};base64,{base64}\" alt=\"{Path.GetFileNameWithoutExtension(blobClient.Name)}\"/>"; } Imports System.IO Imports System.Threading.Tasks Public Function GetImageMimeType(blobName As String) As String Dim extension = Path.GetExtension(blobName).ToLower() Select Case extension Case ".jpg", ".jpeg" Return "image/jpeg" Case ".png" Return "image/png" Case ".gif" Return "image/gif" Case ".svg" Return "image/svg+xml" Case ".webp" Return "image/webp" Case Else Return "image/jpeg" ' default fallback End Select End Function Public Async Function CreateImageTagFromBlob(blobClient As BlobClient) As Task(Of String) Using stream As New MemoryStream() Await blobClient.DownloadToAsync(stream) stream.Position = 0 Dim base64 = Convert.ToBase64String(stream.ToArray()) Dim mimeType = GetImageMimeType(blobClient.Name) Return $"<img src=""data:{mimeType};base64,{base64}"" alt=""{Path.GetFileNameWithoutExtension(blobClient.Name)}""/>" End Using End Function $vbLabelText $csharpLabel 如何將 HTML 轉換成 PDF? <! -- --> <!--說明:說明程式碼概念的圖表或截圖 --> 使用 ChromePdfRenderer 的 RenderHtmlAsPdf 方法將 imageTag 轉換為 PDF。 IronPDF 的 Chrome 渲染引擎可在轉換過程中保持圖像質量和定位。 為達到最佳效果,請設定 渲染選項 以控制 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 擷取與 IronPDF 渲染的綜合範例,包括錯誤處理與最佳化: using Azure.Storage.Blobs; using IronPdf; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; public class AzureBlobToPdfConverter { private readonly string _connectionString; private readonly ChromePdfRenderer _renderer; public AzureBlobToPdfConverter(string connectionString) { _connectionString = connectionString; _renderer = new ChromePdfRenderer(); // Configure rendering options for better image quality _renderer.RenderingOptions.ImageQuality = 100; _renderer.RenderingOptions.DpiResolution = 300; } public async Task<PdfDocument> ConvertBlobImagesToPdfAsync(string containerName, List<string> blobNames) { var htmlBuilder = new StringBuilder(); htmlBuilder.Append("<html><body style='margin: 20px;'>"); var blobServiceClient = new BlobServiceClient(_connectionString); var containerClient = blobServiceClient.GetBlobContainerClient(containerName); foreach (var blobName in blobNames) { try { var blobClient = containerClient.GetBlobClient(blobName); var imageTag = await CreateImageTagFromBlob(blobClient); htmlBuilder.Append(imageTag); htmlBuilder.Append("<br/><br/>"); // Add spacing between images } catch (Exception ex) { // Log error and continue with other images Console.WriteLine($"Error processing blob {blobName}: {ex.Message}"); } } htmlBuilder.Append("</body></html>"); // Convert the complete HTML to PDF return _renderer.RenderHtmlAsPdf(htmlBuilder.ToString()); } private async Task<string> CreateImageTagFromBlob(BlobClient blobClient) { using var stream = new MemoryStream(); await blobClient.DownloadToAsync(stream); stream.Position = 0; var base64 = Convert.ToBase64String(stream.ToArray()); var mimeType = GetImageMimeType(blobClient.Name); return $"<img src=\"data:{mimeType};base64,{base64}\" " + $"alt=\"{Path.GetFileNameWithoutExtension(blobClient.Name)}\" " + $"style=\"max-width: 100%; height: auto;\"/>"; } private string GetImageMimeType(string blobName) { var extension = Path.GetExtension(blobName).ToLower(); return extension switch { ".jpg" or ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".svg" => "image/svg+xml", ".webp" => "image/webp", _ => "image/jpeg" }; } } using Azure.Storage.Blobs; using IronPdf; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; public class AzureBlobToPdfConverter { private readonly string _connectionString; private readonly ChromePdfRenderer _renderer; public AzureBlobToPdfConverter(string connectionString) { _connectionString = connectionString; _renderer = new ChromePdfRenderer(); // Configure rendering options for better image quality _renderer.RenderingOptions.ImageQuality = 100; _renderer.RenderingOptions.DpiResolution = 300; } public async Task<PdfDocument> ConvertBlobImagesToPdfAsync(string containerName, List<string> blobNames) { var htmlBuilder = new StringBuilder(); htmlBuilder.Append("<html><body style='margin: 20px;'>"); var blobServiceClient = new BlobServiceClient(_connectionString); var containerClient = blobServiceClient.GetBlobContainerClient(containerName); foreach (var blobName in blobNames) { try { var blobClient = containerClient.GetBlobClient(blobName); var imageTag = await CreateImageTagFromBlob(blobClient); htmlBuilder.Append(imageTag); htmlBuilder.Append("<br/><br/>"); // Add spacing between images } catch (Exception ex) { // Log error and continue with other images Console.WriteLine($"Error processing blob {blobName}: {ex.Message}"); } } htmlBuilder.Append("</body></html>"); // Convert the complete HTML to PDF return _renderer.RenderHtmlAsPdf(htmlBuilder.ToString()); } private async Task<string> CreateImageTagFromBlob(BlobClient blobClient) { using var stream = new MemoryStream(); await blobClient.DownloadToAsync(stream); stream.Position = 0; var base64 = Convert.ToBase64String(stream.ToArray()); var mimeType = GetImageMimeType(blobClient.Name); return $"<img src=\"data:{mimeType};base64,{base64}\" " + $"alt=\"{Path.GetFileNameWithoutExtension(blobClient.Name)}\" " + $"style=\"max-width: 100%; height: auto;\"/>"; } private string GetImageMimeType(string blobName) { var extension = Path.GetExtension(blobName).ToLower(); return extension switch { ".jpg" or ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".svg" => "image/svg+xml", ".webp" => "image/webp", _ => "image/jpeg" }; } } Imports Azure.Storage.Blobs Imports IronPdf Imports System Imports System.Collections.Generic Imports System.IO Imports System.Text Imports System.Threading.Tasks Public Class AzureBlobToPdfConverter Private ReadOnly _connectionString As String Private ReadOnly _renderer As ChromePdfRenderer Public Sub New(connectionString As String) _connectionString = connectionString _renderer = New ChromePdfRenderer() ' Configure rendering options for better image quality _renderer.RenderingOptions.ImageQuality = 100 _renderer.RenderingOptions.DpiResolution = 300 End Sub Public Async Function ConvertBlobImagesToPdfAsync(containerName As String, blobNames As List(Of String)) As Task(Of PdfDocument) Dim htmlBuilder As New StringBuilder() htmlBuilder.Append("<html><body style='margin: 20px;'>") Dim blobServiceClient As New BlobServiceClient(_connectionString) Dim containerClient = blobServiceClient.GetBlobContainerClient(containerName) For Each blobName In blobNames Try Dim blobClient = containerClient.GetBlobClient(blobName) Dim imageTag = Await CreateImageTagFromBlob(blobClient) htmlBuilder.Append(imageTag) htmlBuilder.Append("<br/><br/>") ' Add spacing between images Catch ex As Exception ' Log error and continue with other images Console.WriteLine($"Error processing blob {blobName}: {ex.Message}") End Try Next htmlBuilder.Append("</body></html>") ' Convert the complete HTML to PDF Return _renderer.RenderHtmlAsPdf(htmlBuilder.ToString()) End Function Private Async Function CreateImageTagFromBlob(blobClient As BlobClient) As Task(Of String) Using stream As New MemoryStream() Await blobClient.DownloadToAsync(stream) stream.Position = 0 Dim base64 = Convert.ToBase64String(stream.ToArray()) Dim mimeType = GetImageMimeType(blobClient.Name) Return $"<img src=""data:{mimeType};base64,{base64}"" " & $"alt=""{Path.GetFileNameWithoutExtension(blobClient.Name)}"" " & $"style=""max-width: 100%; height: auto;""/>" End Using End Function Private Function GetImageMimeType(blobName As String) As String Dim extension = Path.GetExtension(blobName).ToLower() Return extension Select Case extension Case ".jpg", ".jpeg" Return "image/jpeg" Case ".png" Return "image/png" Case ".gif" Return "image/gif" Case ".svg" Return "image/svg+xml" Case ".webp" Return "image/webp" Case Else Return "image/jpeg" End Select End Function End Class $vbLabelText $csharpLabel 效能考量 <! -- --> <!--說明:顯示效能指標和最佳化結果的儀表板 --> 在處理大型圖片或多個 blob 時,請執行 async 和多執行緒技術,以提高效能。 加入快取機制,避免重複下載相同的 blob。 對於生產環境,尤其是 Azure 部署,請檢閱 IronPDF 的 Azure 部署指南,以瞭解最佳實務和組態建議。 對於記憶體密集的作業,請使用IronPDF的記憶體串流功能來最佳化資源使用。 安全性與驗證 在存取 Azure Blob Storage 時確保正確的驗證。 為了增強安全性,在存取受保護的資源時,請執行 自訂 HTTP 標頭。 考慮對包含 Azure blob 影像的敏感文件實施 PDF 密碼保護。 疑難排解常見問題 如果遇到 blob 儲存整合問題,請參閱 IronPDF 的 Azure 疑難排解指南,以瞭解常見問題的解決方案。 針對影像的特定問題,影像呈現文件提供了處理各種情況的詳細指引。 常見問題解答 如何使用 Azure Blob Storage 中儲存的影像生成 PDF? 若要使用 Azure Blob Storage 影像產生 PDF,請以二進位方式擷取 Blob 資料,將其轉換為 base64 字串,嵌入 HTML img 標籤中,並使用 IronPDF 的 ChromePdfRenderer 將 HTML 轉換為 PDF。此方法可與 IronPDF 的 HTML 至 PDF 轉換功能無縫配合,同時保持影像品質。 將 Azure Blob 影像渲染成 PDF 的最快方法是什麼? 最快速的方法是使用 IronPDF 的單行方法:使用 BlobContainerClient 擷取 Blob,使用 Convert.ToBase64String() 將其轉換為 base64,將其嵌入 img 標籤中,並使用 IronPDF 的 ChromePdfRenderer().RenderHtmlAsPdf() 方法進行渲染。 為什麼我不能在 PDF 中使用 Azure Blob Storage 影像的直接檔案參考? Azure Blob Storage 需要處理二進位資料格式,而非直接的檔案參照。解決方案是將圖片轉換為 base64 字串,並將其嵌入 img 標籤中,IronPDF 便可透過其 HTML 至 PDF 的轉換功能處理這些圖片。 要在 PDF 中使用 Azure Blob 影像,我需要哪些 NuGet 套件? 您將需要 Azure.Storage.Blobs NuGet 套件來進行 blob 儲存作業,以及 IronPDF 來進行 PDF 渲染。IronPDF 提供 ChromePdfRenderer,可將內嵌 base64 圖片的 HTML 轉換為 PDF 文件。 在生成 PDF 時,如何處理安全 Azure Blob 存取的驗證? 在 C# 專案中設定 Azure Storage 帳戶,並進行適當的驗證與連接。對於複雜的認證情境,您可以探索 IronPDF 的 HTTP 請求標頭功能,以處理渲染 PDF 時的安全 blob 存取。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 17,386,124 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:17,386,124 查看許可證