使用 MemoryStream 光柵化為圖像
如何使用MemoryStream將PDF頁面轉換為圖像,而不涉及文件系統
IronPDF 提供從 MemoryStream 加載 PDF 文件的功能。 ( 操作指南 API 參考文獻)
使用 PdfDocument.ToBitMap()
將 PDF 頁面匯出為圖像的方法。這將返回一個 IronSoftware.Drawing.AnyBitmap 對象陣列,可用於進一步處理。
using IronPdf;
// Example rendering PDF documents to Images or Thumbnails
using var pdf = PdfDocument.FromFile("Example.pdf");
// or make one using Chrome Renderer
IronSoftware.Drawing.AnyBitmap[] pageImages = pdf.ToBitmap();
foreach (var bitmap in pageImages)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// save to PNG
bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png);
// use memoryStream
}
bitmap.Dispose();
}
using IronPdf;
// Example rendering PDF documents to Images or Thumbnails
using var pdf = PdfDocument.FromFile("Example.pdf");
// or make one using Chrome Renderer
IronSoftware.Drawing.AnyBitmap[] pageImages = pdf.ToBitmap();
foreach (var bitmap in pageImages)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// save to PNG
bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png);
// use memoryStream
}
bitmap.Dispose();
}
Imports IronPdf
' Example rendering PDF documents to Images or Thumbnails
Private pdf = PdfDocument.FromFile("Example.pdf")
' or make one using Chrome Renderer
Private pageImages() As IronSoftware.Drawing.AnyBitmap = pdf.ToBitmap()
For Each bitmap In pageImages
Using memoryStream As New MemoryStream()
' save to PNG
bitmap.ExportStream(memoryStream, IronSoftware.Drawing.AnyBitmap.ImageFormat.Png)
' use memoryStream
End Using
bitmap.Dispose()
Next bitmap
有用的 Stack Overflow 文章.