gpt4 book ai didi

c# - 如何使用 PDFSharp 从 PDF 中提取 FlateDecoded 图像

转载 作者:行者123 更新时间:2023-11-30 12:50:02 44 4
gpt4 key购买 nike

如何使用 PDFSharp 从 PDF 文档中提取经过 FlateDecoded(例如 PNG)的图像?

我在 PDFSharp 示例中发现了该评论:

// TODO: You can put the code here that converts vom PDF internal image format to a
// Windows bitmap
// and use GDI+ to save it in PNG format.
// [...]
// Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.

有人能解决这个问题吗?

感谢您的回复。

编辑:因为我无法在 8 小时内回答我自己的问题,所以我这样做了:

感谢您的快速回复。

我在“ExportAsPngImage”方法中添加了一些代码,但没有得到想要的结果。它只是提取了一些更多的图像 (png),它们没有正确的颜色并且被扭曲了。

这是我的实际代码:

PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
byte[] decodedBytes = flate.Decode(bytes);

System.Drawing.Imaging.PixelFormat pixelFormat;

switch (bitsPerComponent)
{
case 1:
pixelFormat = PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}

Bitmap bmp = new Bitmap(width, height, pixelFormat);
var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bmpData.Stride;
Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
}
bmp.UnlockBits(bmpData);
using (FileStream fs = new FileStream(@"C:\Export\PdfSharp\" + String.Format("Image{0}.png", count), FileMode.Create, FileAccess.Write))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}

这是正确的方法吗?还是我应该选择另一种方式?非常感谢!

最佳答案

我知道这个答案可能要晚几年,但也许会对其他人有所帮助。

在我的例子中出现了这种困惑,因为 image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent) 似乎没有返回正确的值。作为Vive la déraison根据您的问题指出,您将获得使用 Marshal.Copy 的 BGR 格式。因此,在执行 Marshal.Copy 后反转字节并旋转位图将完成这项工作。

生成的代码如下所示:

private static void ExportAsPngImage(PdfDictionary image, ref int count)
{
int width = image.Elements.GetInteger(PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfImage.Keys.Height);

var canUnfilter = image.Stream.TryUnfilter();
byte[] decodedBytes;

if (canUnfilter)
{
decodedBytes = image.Stream.Value;
}
else
{
PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
decodedBytes = flate.Decode(image.Stream.Value);
}

int bitsPerComponent = 0;
while (decodedBytes.Length - ((width * height) * bitsPerComponent / 8) != 0)
{
bitsPerComponent++;
}

System.Drawing.Imaging.PixelFormat pixelFormat;
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 16:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppArgb1555;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
case 32:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
break;
case 64:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppArgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}

decodedBytes = decodedBytes.Reverse().ToArray();

Bitmap bmp = new Bitmap(width, height, pixelFormat);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
int length = (int)Math.Ceiling(width * (bitsPerComponent / 8.0));
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bmpData.Stride;
Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
}
bmp.UnlockBits(bmpData);
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmp.Save(String.Format("exported_Images\\Image{0}.png", count++), System.Drawing.Imaging.ImageFormat.Png);
}

代码可能需要一些优化,但在我的例子中它确实正确地导出了 FlateDecoded Images。

关于c# - 如何使用 PDFSharp 从 PDF 中提取 FlateDecoded 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10024908/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com