如何将资源图像与 PDFsharp 一起使用?

How do I use a Resources Image with PDFsharp?

我正在使用 PDFsharp 库对 PDF 文件进行一些简单的操作。
我有以下代码将图像从文件夹复制到现有 PDF 文档中 - 它按预期工作:

public void AddImagePDF()
{
  this.DrawPage(this.PDFdoc.Pages[0]);
  this.DrawPage(this.PDFdoc.Pages[1]);
  this.DrawPage(this.PDFdoc.Pages[2]);
}
private void DrawPage(PdfPage page)
{
  XGraphics gfx = XGraphics.FromPdfPage(page);
  DrawPng(gfx);
}
private void DrawPng(XGraphics gfx)
{

  XImage imageMu = XImage.FromFile(@"C:\Images\AnImage.png");
  double width = imageMu.PixelWidth * 7.0 / imageMu.HorizontalResolution;
  double height = imageMu.PixelHeight * 7.0 / imageMu.HorizontalResolution;
  gfx.DrawImage(imageMu,500,30,width,height); 

  this.PDFdoc.Save(this.DestinationFullPath);
}

为了使解决方案更便携,我已将图像文件 AnImage.png 移动到项目资源中 - 此处:

Properties.Resources.AnImage

但是我需要对代码进行哪些更改才能使用资源文件而不是保存在 C 盘中的文件?

获取图片资源流后即可使用XImage.FromStream

顺便说一句:如果您只创建一次 XImage 并将其用于所有页面,它会更有效率并且可能会创建更小的 PDF。

你可以使用pdfsharp的方法FromGdiPlusImage,像这样:

XImage imageMu = XImage.FromGdiPlusImage(Properties.Resources.AnImage);

答案来自另一个 Stack Overflow post,答案在:

How to get path of Properties.Resources.Image in .NET

如果正如您在评论中所说,您不能使用 FromGdiPlusImage 可以选择将其作为流加载,这是从另一个 Stack Overflow post:

System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("Properties.Resources.AnImage.png");
Ximage yourImage = XImage.FromStream(file);

Load image from resources area of project in C# - David Icardi 的回答

将资源转换为字节:

            using System;
            using System.Drawing;
            using System.IO;
            using ITNETAWF.Properties; //WinForms Project

            using PDFDocument = MigraDoc.DocumentObjectModel.Document;
            using PDFImage = MigraDoc.DocumentObjectModel.Shapes.Image;
            using PDFRelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal;
            using PDFRelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical;
            using PDFSection = MigraDoc.DocumentObjectModel.Section;
            using PDFShapePosition = MigraDoc.DocumentObjectModel.Shapes.ShapePosition;
            using PDFWrapStyle = MigraDoc.DocumentObjectModel.Shapes.WrapStyle;


            namespace PdfSharpImage
            {
                public class Class1
                {
                    private void CreatePage()
                    {
                        byte[] zbytData;
                        Bitmap zbmpData;
                        string zstrDataB64;
                        MemoryStream zmstFlujoMemoria;
                        PDFDocument document;
                        PDFImage image;
                        PDFSection section;


                        // Each MigraDoc document needs at least one section.
                        document = new PDFDocument();
                        section = document.AddSection();

                        // Put a logo in the header
                        //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                        //  Logo MFG 225x32.png is an image in the .resx file 
                        //  Logo MFG 225x32.png  width/height=107/32 pixel  width/height=2.83/0.85 cm  width/height=1.11/0.33 inch
                        //  
                        //  Resources.resx file Code:
                        //  <data name="Logo MFG 225x32" type="System.Resources.ResXFileRef, System.Windows.Forms">
                        //    <value>..\Resources\Logo MFG 225x32.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
                        //  </data>
                        //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                        zbmpData = Resources.ResourceManager.GetObject("Logo MFG 225x32") as Bitmap;
                        zbytData = new byte[13750]; // 13750 - The size in bytes of the Image
                        zmstFlujoMemoria = new MemoryStream(zbytData);
                        zbmpData.Save(zmstFlujoMemoria, System.Drawing.Imaging.ImageFormat.Bmp);
                        zstrDataB64 = String.Format("base64:{0}", Convert.ToBase64String(zbytData));
                        //image = section.Headers.Primary.AddImage("../../PowerBooks.png");
                        image = section.Headers.Primary.AddImage(zstrDataB64);
                        //image.Height = "2.5cm";
                        image.Height = "1.11cm";
                        image.LockAspectRatio = true;
                        image.RelativeVertical = PDFRelativeVertical.Line;
                        image.RelativeHorizontal = PDFRelativeHorizontal.Margin;
                        image.Top = PDFShapePosition.Top;
                        image.Left = PDFShapePosition.Right;
                        image.WrapFormat.Style = PDFWrapStyle.Through;
                    }
                }
            }
            // http://www.pdfsharp.net/wiki/Invoice-sample.ashx
            // http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx