我正在使用PdfPig在控制台应用程序(.Net Core)项目中使用C#进行实验。
下面的代码打算从PDF中获取图像并将它们保存到我的桌面上:
IEnumerable<IPdfImage> images = page.GetImages();
foreach (IPdfImage image in images)
{
Console.WriteLine(image);
byte[] bitmap = image.RawBytes.ToArray();
string exportPath = @"C:\Users\ed\Desktop\" + Guid.NewGuid().ToString() + ".jpg";
System.IO.File.WriteAllBytes(exportPath, bitmap);
}
从控制台输出中,我可以看到两个图像是从我正在测试的PDF中提取的:
XObject Image (w 595.1999999999999, h 841.92): <Type, /XObject>, <Subtype, /Image>, <Width, 2480>, <Height, 3508>, <BitsPerComponent, 1>, <ColorSpace, /DeviceGray>, <Filter, /CCITTFaxDecode>, <DecodeParms, <Columns, 2480>, <Rows, 3508>>, <Length, 381787>
XObject Image (w 595.1999999999999, h 841.92): <Type, /XObject>, <Subtype, /Image>, <Width, 2480>, <Height, 3508>, <BitsPerComponent, 1>, <ColorSpace, /DeviceGray>, <Filter, /CCITTFaxDecode>, <DecodeParms, <Columns, 2480>, <Rows, 3508>>, <Length, 241054>
我的桌面上也创建了两个.jpg文件;但是,当我打开该文件时,它的输出出现了问题。例如,我得到了错误消息:
97266a2b-5e2b-49e1-8b19-f6716f294a4d.jpg it appears that we don't support this file format.
请有人建议如何修改这一点;我怀疑我是如何创建图像文件的问题。
谢谢!
发布于 2022-08-10 10:50:55
试试这段代码
IReadOnlyList<byte> bytes = Array.Empty<byte>();
if (imageItem.TryGetBytes(out bytes))
{
bytes = imageItem.RawBytes;
}
using var stream = new MemoryStream(bytes.ToArray());
using var image = SixLabors.ImageSharp.Image.Load(stream, out IImageFormat format);
image.SaveAsJpeg(pathToFileJpeg);
https://stackoverflow.com/questions/67649319
复制相似问题