Przejdź do treści stopki
KORZYSTANIE Z IRONOCR

C# Konwersja PDF na obraz: Kompletny przewodnik dla deweloperów

Converting PDFs to images in C# is pretty common. You might want thumbnails, web previews, or even archive copies. With IronPDF, this process becomes an easy task. This is thanks to its RasterizeToImageFiles method, which lets you convert PDF files into image files like PNG images, JPEG images, TIFF images, or BMP with just a few lines of code.

In this article, we'll walk through everything you need to know to convert PDFs to PNG, JPG, TIFF, or BMP. You'll see how to handle full documents, specific page ranges, and even web pages rendered as PDFs. By the end, you'll have a solid workflow for generating high-quality images from PDFs in your .NET projects.

Why Convert PDF Documents to Images in C#?

Converting PDF pages to images has practical applications in modern .NET Framework or .NET apps. Document management systems need thumbnails for fast visual navigation, while web apps benefit from image formats for better browser compatibility and faster load times.

Additionally, converting PDFs to images ensures your PDF looks right on any platform with limited PDF library support. Whether you're working with multiple pages or a single page, IronPDF handles this process in just a few lines of code without worrying about errors or complicated rendering.

Pierwsze kroki z IronPDF

First, create a new C# console application in Visual Studio and install IronPDF via NuGet Package Manager:

Install-Package IronPDF

C# Convert PDF to Image: Complete Developer Guide: Image 1 - IronPDF being Installed via the NuGet Package Manager Console

IronPDF obsługuje .NET Framework, .NET Core i .NET 5+, zapewniając kompatybilność procesu konwersji plików PDF na obrazy z każdą używaną wersją .NET. Po zainstalowaniu możesz rozpocząć konwersję stron PDF na pliki graficzne w swoim programie.

Jak przekonwertować strony PDF na pliki graficzne?

The simplest way to convert a PDF to images involves loading the document and calling the RasterizeToImageFiles method:

using IronPdf;
class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        var pdfDocument = PdfDocument.FromFile("report.pdf");
        // Convert all pages to PNG images
        pdfDocument.RasterizeToImageFiles(@"C:\images\page_*.png");
    }
}
using IronPdf;
class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        var pdfDocument = PdfDocument.FromFile("report.pdf");
        // Convert all pages to PNG images
        pdfDocument.RasterizeToImageFiles(@"C:\images\page_*.png");
    }
}
Imports IronPdf

Class Program
    Shared Sub Main(args As String())
        ' Load an existing PDF document
        Dim pdfDocument = PdfDocument.FromFile("report.pdf")
        ' Convert all pages to PNG images
        pdfDocument.RasterizeToImageFiles("C:\images\page_*.png")
    End Sub
End Class
$vbLabelText   $csharpLabel

Ten kod konwertuje każdą stronę pliku PDF na oddzielny plik PNG. Gwiazdka (*) w szablonie nazwy pliku jest automatycznie zastępowana numerem strony. For proper resource management, wrap the PdfDocument in a using statement to ensure disposal.

Po uruchomieniu kodu w katalogu wyjściowym widzimy, że mimo iż nasz plik PDF zawiera wiele stron, nasz kod nie określa, które strony mają zostać przekonwertowane, więc każda z nich została zapisana jako osobny plik graficzny:

C# Convert PDF to Image: Complete Developer Guide: Image 2 - PDF to Image example for converting all pages

Aby przekonwertować konkretne strony, należy określić zakres stron:

// Specify the page indexes for conversion
int[] pageIndexes = new[] { 0, 1, 2 };
// Convert pages 1-3 to JPG images
pdfDocument.RasterizeToImageFiles(@"C:\images\page_*.jpg", pageIndexes);
// Specify the page indexes for conversion
int[] pageIndexes = new[] { 0, 1, 2 };
// Convert pages 1-3 to JPG images
pdfDocument.RasterizeToImageFiles(@"C:\images\page_*.jpg", pageIndexes);
' Specify the page indexes for conversion
Dim pageIndexes As Integer() = {0, 1, 2}
' Convert pages 1-3 to JPG images
pdfDocument.RasterizeToImageFiles("C:\images\page_*.jpg", pageIndexes)
$vbLabelText   $csharpLabel

Należy pamiętać, że indeksowanie stron zaczyna się od 0, więc pierwsza strona ma pageIndex = 0.

C# Convert PDF to Image: Complete Developer Guide: Image 3 - Specified PDF pages converted to image

Jak kontrolować jakość obrazu?

Jakość obrazu ma bezpośredni wpływ na rozmiar pliku i przejrzystość wizualną. IronPDF pozwala kontrolować to poprzez ustawienia DPI (punktów na cal):

// High-quality conversion at 300 DPI
pdfDocument.RasterizeToImageFiles(@"C:\images\high_quality_*.png", DPI: 300);
// Web-optimized at 150 DPI
pdfDocument.RasterizeToImageFiles(@"C:\images\web_*.jpg", DPI: 150);
// High-quality conversion at 300 DPI
pdfDocument.RasterizeToImageFiles(@"C:\images\high_quality_*.png", DPI: 300);
// Web-optimized at 150 DPI
pdfDocument.RasterizeToImageFiles(@"C:\images\web_*.jpg", DPI: 150);
' High-quality conversion at 300 DPI
pdfDocument.RasterizeToImageFiles("C:\images\high_quality_*.png", DPI:=300)
' Web-optimized at 150 DPI
pdfDocument.RasterizeToImageFiles("C:\images\web_*.jpg", DPI:=150)
$vbLabelText   $csharpLabel

Domyślna rozdzielczość 96 DPI wystarcza do podstawowego podglądu, ale należy ją zwiększyć do 150–300 DPI w przypadku obrazów o jakości PRINT. Wyższe wartości DPI zapewniają ostrzejszy obraz, ale powodują zwiększenie rozmiaru plików.

C# Convert PDF to Image: Complete Developer Guide: Image 4 - High-quality converted PDF beside the original

Jakie formaty obrazów są obsługiwane?

IronPDF supports multiple image formats through the ImageType parameter:

// Convert to different formats
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.png", IronPdf.Imaging.ImageType.Png);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.jpg", IronPdf.Imaging.ImageType.Jpeg);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.tiff", IronPdf.Imaging.ImageType.Tiff);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.bmp", IronPdf.Imaging.ImageType.Bitmap);
// Convert to different formats
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.png", IronPdf.Imaging.ImageType.Png);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.jpg", IronPdf.Imaging.ImageType.Jpeg);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.tiff", IronPdf.Imaging.ImageType.Tiff);
pdfDocument.RasterizeToImageFiles(@"C:\images\output_*.bmp", IronPdf.Imaging.ImageType.Bitmap);
' Convert to different formats
pdfDocument.RasterizeToImageFiles("C:\images\output_*.png", IronPdf.Imaging.ImageType.Png)
pdfDocument.RasterizeToImageFiles("C:\images\output_*.jpg", IronPdf.Imaging.ImageType.Jpeg)
pdfDocument.RasterizeToImageFiles("C:\images\output_*.tiff", IronPdf.Imaging.ImageType.Tiff)
pdfDocument.RasterizeToImageFiles("C:\images\output_*.bmp", IronPdf.Imaging.ImageType.Bitmap)
$vbLabelText   $csharpLabel

Wybierz format PNG dla obrazów wymagających przezroczystości, JPEG dla zdjęć i treści internetowych, TIFF do celów archiwizacyjnych oraz BMP, gdy potrzebne są obrazy nieskompresowane. The IronPDF API reference provides detailed information about all supported ImageType options.

Jak radzić sobie w zaawansowanych sytuacjach?

IronPDF doskonale radzi sobie ze złożonymi scenariuszami konwersji plików PDF na obrazy. Jedną ze szczególnie przydatnych funkcji jest konwersja stron internetowych bezpośrednio na obrazy poprzez renderowanie do formatu PDF. For more HTML conversion options, explore the HTML to PDF conversion guide:

// Convert a webpage to images
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument webPdf = renderer.RenderUrlAsPdf("https://apple.com");
webPdf.RasterizeToImageFiles(@"C:\images\webpage_*.png", DPI: 200);
// Convert a webpage to images
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument webPdf = renderer.RenderUrlAsPdf("https://apple.com");
webPdf.RasterizeToImageFiles(@"C:\images\webpage_*.png", DPI: 200);
Imports IronPdf

' Convert a webpage to images
Dim renderer As New ChromePdfRenderer()
Dim webPdf As PdfDocument = renderer.RenderUrlAsPdf("https://apple.com")
webPdf.RasterizeToImageFiles("C:\images\webpage_*.png", DPI:=200)
$vbLabelText   $csharpLabel

Takie podejście doskonale oddaje dynamiczne treści internetowe, zachowując wszystkie elementy stylistyczne i renderowane przez JavaScript.

Aby przetwarzać wiele plików PDF w trybie wsadowym, należy zaimplementować prostą pętlę:

string[] pdfFiles = Directory.GetFiles(@"C:\pdfs", "*.pdf");
foreach (string pdfPath in pdfFiles)
{
    using (var pdf = PdfDocument.FromFile(pdfPath))
    {
        string outputPath = Path.Combine(@"C:\images",
            Path.GetFileNameWithoutExtension(pdfPath) + "_*.png");
        pdf.RasterizeToImageFiles(outputPath);
    }
}
string[] pdfFiles = Directory.GetFiles(@"C:\pdfs", "*.pdf");
foreach (string pdfPath in pdfFiles)
{
    using (var pdf = PdfDocument.FromFile(pdfPath))
    {
        string outputPath = Path.Combine(@"C:\images",
            Path.GetFileNameWithoutExtension(pdfPath) + "_*.png");
        pdf.RasterizeToImageFiles(outputPath);
    }
}
Imports System.IO

Dim pdfFiles As String() = Directory.GetFiles("C:\pdfs", "*.pdf")
For Each pdfPath As String In pdfFiles
    Using pdf = PdfDocument.FromFile(pdfPath)
        Dim outputPath As String = Path.Combine("C:\images", Path.GetFileNameWithoutExtension(pdfPath) & "_*.png")
        pdf.RasterizeToImageFiles(outputPath)
    End Using
Next pdfPath
$vbLabelText   $csharpLabel

Wnioski

IronPDF ułatwia konwersję dokumentów PDF na pliki graficzne w języku C#. Whether you're creating thumbnails, PNG images, JPEG images, or handling TIFF conversion for multiple pages, the RasterizeToImageFiles method handles everything.

You can customize output formats, control image quality with DPI settings, and even convert web pages rendered as PDFs into images, all without complex setup. For more advanced PDF features, check IronPDF's extensive documentation that includes sample code and explanations to explore everything IronPDF can do.

Chcesz wdrożyć konwersję plików PDF na obrazy w swojej aplikacji napisanej w języku C#? Start with a free trial to explore IronPDF's full capabilities, including watermark-free production testing for 30 days. Need to convert documents at scałe? Consider a commercial license for unlimited conversions and priority support.

Często Zadawane Pytania

Jak przekonwertować plik PDF na obraz w języku C#?

Możesz użyć biblioteki IronPDF do konwersji pliku PDF na obraz w języku C#. Dzięki metodzie `RasterizeToImageFiles` możesz łatwo konwertować pliki PDF na formaty obrazów, takie jak PNG, JPEG, TIFF lub BMP.

Jakie formaty obrazów są obsługiwane przez IronPDF do konwersji plików PDF?

IronPDF obsługuje kilka formatów obrazów podczas konwersji plików PDF, w tym PNG, JPEG, TIFF i BMP.

Czy za pomocą IronPDF można tworzyć miniatury z plików PDF?

Tak, można tworzyć miniatury z dokumentów PDF za pomocą IronPDF, konwertując pliki PDF na obrazy o mniejszym rozmiarze, takie jak JPEG lub PNG.

Czy IronPDF może konwertować całe dokumenty PDF na obrazy?

Tak, IronPDF może konwertować całe dokumenty PDF na obrazy, umożliwiając zapisanie każdej strony jako osobnego pliku graficznego.

Czym jest metoda `RasterizeToImageFiles` w IronPDF?

Metoda `RasterizeToImageFiles` w IronPDF służy do konwersji plików PDF na różne formaty obrazów, takie jak PNG, JPEG, TIFF i BMP, ułatwiając tworzenie obrazówych reprezentacji treści plików PDF.

Dlaczego miałbym potrzebować konwersji pliku PDF na obraz w języku C#?

Konwersja pliku PDF na obraz może być przydatna do tworzenia miniatur, podglądów internetowych lub archiwizacji, ułatwiając udostępnianie i wyświetlanie treści plików PDF.

Ile linii kodu potrzeba, aby przekonwertować plik PDF na obraz za pomocą IronPDF?

Za pomocą IronPDF można przekonwertować plik PDF na obraz za pomocą zaledwie kilku wierszy kodu, wykorzystując metodę `RasterizeToImageFiles`.

Kannaopat Udonpant
Inżynier oprogramowania
Zanim stał się inżynierem oprogramowania, Kannapat ukończył doktorat z zasobów środowiskowych na Uniwersytecie Hokkaido w Japonii. W czasie studiowania, Kannapat również został członkiem Laboratorium Robotyki Pojazdów, które jest częścią Wydziału Inżynierii Bioprodukcji. W 2022 roku wykorzystał swoje umiejętności w ...
Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie