With HiQPdf Library you can extract the images from PDF documents using the HiQPdfPdfImagesExtract class. You can extract the images to memory HiQPdfExtractedPdfImage objects using the PdfImagesExtractExtractToImageObjects(String, Int32, Int32) method or to image files using the PdfImagesExtractExtractToImageFiles(String, Int32, Int32, String, String) method. There is also a special method PdfImagesExtractRaiseImageExtractedEvent(String, Int32, Int32) which can be used to extract the images one by one in memory to avoid high memory usage for large PDF documents. The HiQPdfExtractedPdfImage objects can be disposed when they are no longer necessary.
In this demo you can learn how to extract the images from a PDF document. You can choose the range of PDF pages from where to extract the images. Images will be extracted in memory in .NET Image objects.
protected void buttonExtractImages_Click(object sender, EventArgs e) { // get the PDF file string pdfFile = Server.MapPath("~") + @"\DemoFiles\Pdf\InputPdf.pdf"; // create the PDF images extractor PdfImagesExtract pdfImagesExtract = new PdfImagesExtract(); int fromPdfPageNumber = int.Parse(textBoxFromPage.Text); int toPdfPageNumber = textBoxToPage.Text.Length > 0 ? int.Parse(textBoxToPage.Text) : 0; // extract the images from PDF document to memory in .NET Image objects // the images can also be extracted to a folder using the ExtractToImageFiles method // or they can be extracted one by one using the RaiseImageExtractedEvent method ExtractedPdfImage[] extractedImages = pdfImagesExtract.ExtractToImageObjects(pdfFile, fromPdfPageNumber, toPdfPageNumber); // return if no image was extracted if (extractedImages.Length == 0) return; // get the largest image bytes in a buffer byte[] imageBuffer = null; try { // select the largest image ExtractedPdfImage largestImage = null; for (int i = 0; i < extractedImages.Length; i++) { if (largestImage == null || extractedImages[i].ImageObject.Size.Width * extractedImages[i].ImageObject.Size.Height > largestImage.ImageObject.Size.Width * largestImage.ImageObject.Size.Height) { largestImage = extractedImages[i]; } } // get the .NET Image object System.Drawing.Image imageObject = largestImage.ImageObject; // get the image data in a buffer imageBuffer = GetImageBuffer(imageObject); } finally { // dispose the extracted images for (int i = 0; i < extractedImages.Length; i++) extractedImages[i].Dispose(); } // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "image/png"); // let the browser know how to open the image and the image name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}; size={1}", "ExtractedImage", imageBuffer.Length.ToString())); // write the image buffer to HTTP response HttpContext.Current.Response.BinaryWrite(imageBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); }