使用 MemoryStream 将 PDF 栅格化为图像
This article was translated from English: Does it need improvement?
TranslatedView the article in English
如何使用 MemoryStream 将 PDF 页面转换为图像,而无需触及文件系统?
IronPDF 提供从 MemoryStream 加载 PDF 文档的功能。 (How-Tos |API 参考)
使用 PdfDocument.ToBitmap() 方法将 PDF 页导出为图像。 这将返回一个 Iron Software.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 的更多信息。
准备开始了吗?
Nuget 下载 16,685,821 | 版本: 2025.12 刚刚发布






