如何在 C# 中借助 PDFsharp 将 PictureBox 图像显示为 PDF 文件?

How to display the PictureBox image into PDF file with the help of PDFsharp in c#?

我正在用 c# 开发 Windows 应用程序。在我的应用程序中,我在 PDFsharp 的帮助下开发了 PDF 文件。我必须在 PictureBox 的 PDF 文件中显示图像。

临时将图像保存到本地磁盘并添加到 pdf 文档对象(创建 pdf 文档后您可以删除图像。)

示例代码:

Bitmap bmpImage = null;
string strPdfDocSavePath = @"D:\Images\";
bmpImage = PctrBx.Image;
string strImageName = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
bmpImage.Save(strPdfDocSavePath + @"\" + strImageName + ".bmp");

MigraDoc.DocumentObjectModel.Shapes.Image img = row.Cells[0].AddImage(strPdfDocSavePath + @"\" + strImageName + ".bmp"); // Here i am using table for displaying image , add column and row to table, add image to desired cell
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
img.LockAspectRatio = true;
img.WrapFormat.Style =MigraDoc.DocumentObjectModel.Shapes.WrapStyle.Through;
img.Width = "7cm";

最终在创建 pdf 后删除图像

使用PDFsharp的GDI+时,只需调用XImage.FromGdiPlusImage:

XImage img = XImage.FromGdiPlusImage(pb.Image);

pb 是图片框。

此答案适用于 PDFsharp。

可以在此处找到使用 PDFsharp 将图像添加到 PDF 文件的示例代码(演示 XImage class 的使用):
http://pdfsharp.net/wiki/Graphics-sample.ashx#Images_35