Azure Blob Storageに保存された画像をC#でレンダリングする方法 | 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 は、Microsoft Azure が提供するクラウドベースのストレージサービスです。 これは、HTTP または HTTPS 経由でアクセスできるテキストやバイナリ データなどの大量の非構造化データを保存するように設計されています。

一部の開発者は Azure Blob Storage に保存されている画像を使用したいと考えています。 これは、画像データがファイルではなくバイナリとして保存されているため、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. Azure Blob に保存された画像をレンダリングするための IronPDF をダウンロードする
  2. ブロブを取得するプロセスを処理する
  3. ToBase64String メソッドを使用してバイトを base64 に変換する
  4. イメージ タグに base64 情報を含める
  5. HTML を PDF にレンダリングする


Azure Blob を HTML に変換する

すでに Azure Storage アカウントをセットアップし、ブロブを持つコンテナーを持っていると仮定して、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

imageTag が使用されている場所に実際の HTML コンテンツを含むように "htmlContent" 変数を調整してください。

よくある質問

Azure Blob Storageからの画像を使用してPDFをレンダリングするにはどうすればいいですか?

Azure Blob Storageからの画像を使用してPDFをレンダリングするには、IronPDFを使用して画像データをbase64文字列に変換し、HTMLのimgタグに埋め込み、それをPDFとしてレンダリングします。

Azure Blob Storageから画像をbase64文字列に変換するために必要な手順は何ですか?

まず、DownloadToStreamAsyncメソッドを使用して画像のblobをダウンロードします。次に、画像ストリームをバイト配列に変換し、Convert.ToBase64Stringでbase64文字列に変換します。

Azure Blob Storageからの画像をHTMLドキュメントに含めるにはどうすればいいですか?

Azure Blob Storageからストリームとして画像をダウンロードし、base64文字列に変換し、HTMLのimgタグに埋め込みます。これにより、画像をHTMLドキュメントに直接含めることができます。

PDFの画像を扱うためのAzure Blob Storageの役割は何ですか?

Azure Blob Storageは非構造化画像データを保存するために使用されます。このデータをbase64に変換することで、HTMLに埋め込み、IronPDFを使用してPDFに含めることができます。

C#で埋め込まれた画像付きのHTMLコンテンツをPDFに変換するにはどうすればいいですか?

IronPDFのRenderHtmlAsPdfメソッドを使用して、埋め込まれたbase64画像を含むHTMLコンテンツをPDFに変換できます。このメソッドはHTMLを処理し、PDFドキュメントとしてレンダリングします。

Azure Blob Storageからのblobにアクセスするために認証が必要ですか?

はい、安全にblobへのアクセスと管理するために、C#プロジェクトでAzure Storageアカウントへの接続を確立するための認証が必要です。

画像をPDFレンダリング用にbase64に変換する意義は何ですか?

画像をbase64に変換することで、バイナリ画像データをデータURLとしてHTMLドキュメントに直接埋め込むことが可能となり、IronPDFで正確にPDFとしてレンダリングできます。

IronPDFを使用してPDFをレンダリングし始めるにはどうすればいいですか?

NuGetパッケージマネージャーからIronPDFをダウンロードできます。インストールされたら、HTMLコンテンツを画像も含めてPDFドキュメントにレンダリングするためのメソッドを使用できます。

Azure Blob Storage から画像をレンダリングする場合、IronPDF は .NET 10 アプリケーションと完全に互換性がありますか?

はい。IronPDFは.NET 10と完全に互換性があり、Web、デスクトップ、クラウドサービスなどの.NET 10プロジェクトでHTMLからPDFへのレンダリング(画像、CSS、JavaScriptなどを含む)をサポートします。.NET 10アプリでも、BLOBダウンロードとBase64埋め込みのワークフローをそのまま使用できます。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はいいですか?
Nuget ダウンロード 16,133,208 | バージョン: 2025.11 ただ今リリースされました