【问题标题】:GIF/ISF image in Xfinium.PDF PdfFlowDocumentXfinium.PDF PdfFlowDocument 中的 GIF/ISF 图像
【发布时间】:2018-01-03 16:40:59
【问题描述】:

我正在为 UWP 使用 Xfinium.PDF 组件。我需要显示一个使用 ISF 编码为 GIF 的图像(这里有更多信息:https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/save-and-load-ink)。我只有一个字节流,而不是物理图像。

当我使用 PdfFlowDocument 时,我需要实例化一个 PdfFlowTableImageCell。

我怎样才能做到这一点?

【问题讨论】:

    标签: c# pdf uwp gif


    【解决方案1】:

    您必须使用 BitmapDecoder/BitmapEncoder UWP 类将 GIF 图像转换为 PNG(或 JPEG),然后创建可在 PdfFlowTableImageCell 中使用的 PdfPngImage(或 PdfJpegImage)。

    更新:下面的代码展示了如何在 UWP 中将 GIF 图像转换为 PNG(它假设您的 GIF 图像在流中):

    // Stream gifStream = your image stream
    BitmapDecoder gifDecoder = 
        await BitmapDecoder.CreateAsync(gifStream.AsRandomAccessStream());
    BitmapFrame gifFrame = await gifDecoder.GetFrameAsync(0);
    PixelDataProvider gifPixels = await gifFrame.GetPixelDataAsync();
    
    InMemoryRandomAccessStream pngStream = new InMemoryRandomAccessStream();
    BitmapEncoder pngEncoder = 
        await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngStream);
    pngEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, 
        gifFrame.PixelWidth, gifFrame.PixelHeight, 
        96, 96, gifPixels.DetachPixelData()); 
    await pngEncoder.FlushAsync();
    pngStream.Seek(0);
    
    PdfPngImage pngImage = new PdfPngImage(pngStream.AsStream());
    

    【讨论】:

    • 好的。谢谢米海。我试图弄清楚如何将 GIF/ISF 流(我没有物理文件,只有一个流)转换为 PdfPngImage 所需的 PNG 流。到目前为止,没有运气。
    • @ata6502 我用显示如何将 GIF 图像转换为 PNG 的代码更新了我的答案
    • 谢谢米海。我试过你的代码,但它没有按预期工作。 PNG 图像在 PDF 报告中显示为黑色矩形。经过仔细检查,原来 GIF 源流的长度是 2490 字节,但输出的 PNG 流只有 259 字节。我不知道为什么会有这样的差异。也许是因为 GIF 有 ISF 信息。无论如何,我想向您发送我试图包含在 PDF 中的 GIF 文件,但我在 StackOverflow 上看不到任何选项。
    • @ata6502 请将其发送给我们的支持:xfiniumpdf.com/xfinium-pdf-contact-us.html
    • 谢谢米海。我会在星期一寄出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    相关资源
    最近更新 更多