Rasterizing PDFs to Images with MemoryStream

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

Comment convertir des pages PDF en images en utilisant MemoryStream, sans toucher au système de fichiers ?

IronPDF offre le chargement de documents PDF depuis MemoryStream. (Guides pratiques|Référence API)

Utilisez la méthode PdfDocument.ToBitmap() pour exporter des pages PDF en images. Cela retournera un tableau d'objets IronSoftware.Drawing.AnyBitmap, qui peuvent être utilisés pour un traitement ultérieur.

using IronPdf;
using System.IO;

// Example rendering PDF documents to Images or Thumbnails
using var pdf = PdfDocument.FromFile("Example.pdf");

// Convert each page of the PDF document to a bitmap image
IronSoftware.Drawing.AnyBitmap[] pageImages = pdf.ToBitmap();

foreach (var bitmap in pageImages)
{
    // Use MemoryStream to handle the image data in memory
    using (MemoryStream memoryStream = new MemoryStream())
    {
        // Export the image to MemoryStream as a PNG format
        bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png);

        // MemoryStream can now be used for further processing without touching the file system
        // Example: Send it over a network, save to a database, etc.
    }

    // Dispose of the bitmap once processing is complete to free resources
    bitmap.Dispose();
}
using IronPdf;
using System.IO;

// Example rendering PDF documents to Images or Thumbnails
using var pdf = PdfDocument.FromFile("Example.pdf");

// Convert each page of the PDF document to a bitmap image
IronSoftware.Drawing.AnyBitmap[] pageImages = pdf.ToBitmap();

foreach (var bitmap in pageImages)
{
    // Use MemoryStream to handle the image data in memory
    using (MemoryStream memoryStream = new MemoryStream())
    {
        // Export the image to MemoryStream as a PNG format
        bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png);

        // MemoryStream can now be used for further processing without touching the file system
        // Example: Send it over a network, save to a database, etc.
    }

    // Dispose of the bitmap once processing is complete to free resources
    bitmap.Dispose();
}
Imports IronPdf
Imports System.IO

' Example rendering PDF documents to Images or Thumbnails
Private pdf = PdfDocument.FromFile("Example.pdf")

' Convert each page of the PDF document to a bitmap image
Private pageImages() As IronSoftware.Drawing.AnyBitmap = pdf.ToBitmap()

For Each bitmap In pageImages
	' Use MemoryStream to handle the image data in memory
	Using memoryStream As New MemoryStream()
		' Export the image to MemoryStream as a PNG format
		bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png)

		' MemoryStream can now be used for further processing without touching the file system
		' Example: Send it over a network, save to a database, etc.
	End Using

	' Dispose of the bitmap once processing is complete to free resources
	bitmap.Dispose()
Next bitmap
$vbLabelText   $csharpLabel

Voici un article utile de Stack Overflow article pour plus de détails sur la sauvegarde d'un bitmap dans un MemoryStream.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 16,154,058 | Version : 2025.11 vient de sortir