How can I get the image from a .pdf file into a System.Drawing.Bitmap?
如何将.pdf文件中的图像转换为System.Drawing.Bitmap?
2 个解决方案
#1
You may want to try Docotic.Pdf library for the task.
您可能想要尝试Docotic.Pdf库来完成任务。
Here is a sample that shows how to create System.Drawing.Bitmap
from an image in a PDF file:
下面的示例演示如何从PDF文件中的图像创建System.Drawing.Bitmap:
static void GetImagesFromPdfAsBitmaps()
{
string pathToPdf = "";
using (PdfDocument pdf = new PdfDocument(pathToPdf))
{
for (int i = 0; i < pdf.Images.Count; i++)
{
using (MemoryStream ms = new MemoryStream())
{
pdf.Images[i].Save(ms);
// don't forget to rewind stream
ms.Position = 0;
System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(ms);
// ... use the bitmap and then dispose it
bitmap.Dispose();
}
}
}
}
The library can also save images to files. The library doesn't resample images (i.e. you'll get exactly the same image as in PDF)
该库还可以将图像保存到文件中。该库不会重新取样图像(即您将获得与PDF中完全相同的图像)
Disclaimer: I work for Bit Miracle, vendor of the library.
免责声明:我为图书馆的供应商Bit Miracle工作。
#2
#1
You may want to try Docotic.Pdf library for the task.
您可能想要尝试Docotic.Pdf库来完成任务。
Here is a sample that shows how to create System.Drawing.Bitmap
from an image in a PDF file:
下面的示例演示如何从PDF文件中的图像创建System.Drawing.Bitmap:
static void GetImagesFromPdfAsBitmaps()
{
string pathToPdf = "";
using (PdfDocument pdf = new PdfDocument(pathToPdf))
{
for (int i = 0; i < pdf.Images.Count; i++)
{
using (MemoryStream ms = new MemoryStream())
{
pdf.Images[i].Save(ms);
// don't forget to rewind stream
ms.Position = 0;
System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(ms);
// ... use the bitmap and then dispose it
bitmap.Dispose();
}
}
}
}
The library can also save images to files. The library doesn't resample images (i.e. you'll get exactly the same image as in PDF)
该库还可以将图像保存到文件中。该库不会重新取样图像(即您将获得与PDF中完全相同的图像)
Disclaimer: I work for Bit Miracle, vendor of the library.
免责声明:我为图书馆的供应商Bit Miracle工作。