GIF/ISF 图片在 Xfinium.PDF PdfFlowDocument 中
GIF/ISF image in Xfinium.PDF PdfFlowDocument
我正在为 UWP 使用 Xfinium.PDF 组件。我需要显示使用 ISF 编码为 GIF 的图像(更多信息:https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/save-and-load-ink)。我只有字节流,不是物理图像。
因为我正在使用 PdfFlowDocument,所以我需要实例化一个 PdfFlowTableImageCell。
我怎样才能做到这一点?
您必须使用 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());
我正在为 UWP 使用 Xfinium.PDF 组件。我需要显示使用 ISF 编码为 GIF 的图像(更多信息:https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/save-and-load-ink)。我只有字节流,不是物理图像。
因为我正在使用 PdfFlowDocument,所以我需要实例化一个 PdfFlowTableImageCell。
我怎样才能做到这一点?
您必须使用 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());