Rasteryzacja plików PDF do obrazów za pomocą MemoryStream
Jak przekonwertować strony PDF na obrazy przy użyciu MemoryStream, bez ingerencji w system plików?
IronPDF umożliwia ładowanie dokumentów PDF z MemoryStream. (Poradniki | Dokumentacja API)
Użyj metody PdfDocument.ToBitmap(), aby wyeksportować strony PDF jako obrazy. Zwróci to tablicę obiektów IronSoftware.Drawing.AnyBitmap, które można wykorzystać do dalszego przetwarzania.
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 UsingOto przydatny artykuł na Stack Overflow zawierający więcej szczegółów na temat zapisywania bitmapy w MemoryStream.

Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podręczników.