title: How to add an image watermark in PDFsharp?
body: <p>All samples show how to add a text watermark using PDFsharp. but what if I need to add an image watermark?</p>
  
  <p>I did this:</p>
  
  <pre><code>if (!String.IsNullOrEmpty(_logo) &amp;&amp; System.IO.File.Exists(_logo))
  {
      XImage logo = XImage.FromFile(_logo);
      XRect rectLogo = new XRect(0, 0, page.Width, page.Height);
      rectLogo.Scale(0.5, 0.5);
      gfx.DrawImage(logo, rectLogo);
  }
  </code></pre>
  
  <p>The image appears, but not as a watermark.</p>
  
  <p>EDIT:</p>
  
  <p>I have finally placed a watermark manually, however,there is another thing to solve. How to change opacity of an XImage? I can do it by using an image processor but that is not the idea. This is a software that will use a normal user, and I cannot tell him to modify the image before assigning it as the watermark. Any solution to this?</p>
  
  <p>This is the code at the moment:</p>
  
  <pre><code>            if (!String.IsNullOrEmpty(_logo) &amp;&amp; System.IO.File.Exists(_logo))
              {
                  XImage logo = XImage.FromFile(_logo);
                  double width = logo.PointWidth;
                  double height = logo.PointHeight;
                  double ratio = width / height;
                  if (width &gt; page.Width.Point * 0.5)
                  {
                      width = page.Width.Point * 0.5;
                      height = width / ratio;
                  }
                  else if (height &gt; page.Height.Point * 0.5)
                  {
                      height = page.Height.Point * 0.5;
                      width = height * ratio;
                  }
  
                  double offsetX = (page.Width.Point - width) / 2;
                  double offsetY = (page.Height.Point - height) / 2;
  
                  XRect rectLogo = new XRect(offsetX, offsetY, width, height);
                  gfx.DrawImage(logo, rectLogo);
              }
  </code></pre>