Rasterizing PDFs to Images with MemoryStream

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

如何使用 MemoryStream 将 PDF 页面转换为图像,而无需触及文件系统?

IronPDF 提供从 MemoryStream 加载 PDF 文档的功能。 (操作指南|API 参考)

使用 PdfDocument.ToBitmap() 方法将 PDF 页导出为图像。 这将返回一个 IronSoftware.Drawing.AnyBitmap 对象数组,可用于进一步处理。

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

以下是一个有用的 Stack Overflow 文章,以获取有关将位图保存到 MemoryStream 的更多信息。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布