Rasterizing PDFs to Images with MemoryStream

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

Wie konvertiere ich PDF-Seiten zu Bildern mit MemoryStream, ohne das Dateisystem zu verändern?

IronPDF bietet das Laden von PDF-Dokumenten aus MemoryStream an. (Anleitungen |API-Referenz)

Verwenden Sie die Methode PdfDocument.ToBitmap(), um PDF-Seiten als Bilder zu exportieren. Dies wird ein Array von IronSoftware.Drawing.AnyBitmap-Objekten zurückgeben, die für weitere Verarbeitung verwendet werden können.

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

Hier ist ein hilfreicher Stack Overflow Artikel für weitere Informationen zum Speichern eines Bitmaps in einem MemoryStream.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 16,154,058 | Version: 2025.11 gerade veröffentlicht