gpt4 book ai didi

image - 使用 PdfSharp 从 pdf 中提取 "FLATEDECODE"类型的图像

转载 作者:行者123 更新时间:2023-12-01 05:15:48 24 4
gpt4 key购买 nike

我正在尝试从 pdf 中提取图像。图像是过滤器“FlateDecode”。我得到了图像,但它只是一条黑色路径。我是这方面的新手,请帮助我。代码是:

int width = xObject.Elements.GetInteger(PdfImage.Keys.Width);
int height = xObject.Elements.GetInteger(PdfImage.Keys.Height);
int bitsPerComponent = xObject.Elements.GetInteger (PdfSharp.Pdf.Advanced.PdfImage.Keys.BitsPerComponent);
System.Drawing.Imaging.PixelFormat pixelFormat = new System.Drawing.Imaging.PixelFormat();
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
Bitmap bitmap = new Bitmap(width, height, pixelFormat);
byte[] raw = xObject.Stream.Value;
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
Marshal.Copy(raw, 0, bitmapData.Scan0, raw.Length);
bitmap.UnlockBits(bitmapData);
using (MemoryStream imageStream = new MemoryStream())
{
bitmap.Save(imageStream, ImageFormat.png);
System.Drawing.Image img = System.Drawing.Image.FromStream(imageStream);

img.Save("D:\\trial.png", System.Drawing.Imaging.ImageFormat.png);
}

最佳答案

img.Save("D:\\trial.png", System.Drawing.Imaging.ImageFormat.Jpeg);

不应该是.PNG吗?

关于image - 使用 PdfSharp 从 pdf 中提取 "FLATEDECODE"类型的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254542/

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