使用 MemoryStream 將 PDF 光柵化為圖像
Curtis Chau
Updated: 2026年4月22日
如何使用 MemoryStream 將 PDF 頁面轉換為圖像,而不接觸文件系統?
IronPDF 提供從 MemoryStream 載入 PDF 文件的功能。 (How-Tos | API Reference)
using 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();
}Imports IronPdf
Imports System.IO
' Example rendering PDF documents to Images or Thumbnails
Using pdf = PdfDocument.FromFile("Example.pdf")
' Convert each page of the PDF document to a bitmap image
Dim 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
End Using這裡有一篇有用的 Stack Overflow 文章,提供有關將位圖保存到 MemoryStream 的更多詳細資訊。

技術作家
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
...
閱讀更多