使用 MemoryStream 将 PDF 栅格化为图像

Curtis Chau
Curtis Chau
Updated: 2026年4月22日

如何使用 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();
}

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

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

阅读更多

准备开始了吗?

Nuget Downloads 21,916,531版本:2026.11刚刚发布