0

There is a Windows program that generates PNG from HTML via WebBrowser.

This PNG must then be inserted into a PDF that is opened in Adobe Acrobat Reader DC as a stamp.

Below is a code that draws a PNG and sends it to the clipboard.

static void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var webBrowser = (WebBrowser)sender;

    Bitmap bitmap = new Bitmap(webBrowser.Width, webBrowser.Height, PixelFormat.Format32bppArgb);
    webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
                
    bitmap.MakeTransparent(Color.White);

    Clipboard.SetImage(bitmap);

}

The image is clipboard with a gray background, although when saved via bitmap.Save("img.png", ImageFormat.Png); the background is indeed transparent.

using (MemoryStream stream = new MemoryStream())
    {
        bitmap.Save(stream, ImageFormat.Png);
        var data = new DataObject("PNG", stream);
        Clipboard.Clear();
        Clipboard.SetDataObject(data, true);
    }

Tried saving the image to a MemoryStream and sending it to the clipboard as a DataObject. In this case, the clipboard is left empty.

Thanks to Nyerguds, I know that by default the Windows clipboard does not preserve transparency. But due to lack of experience, I can't figure out how to apply it myself.

How can I keep the transparency of the image for later pasting?

  • Why are you using the clipboard at all? – Dai Mar 10 '22 at 00:16
  • Because using the clipboard in Acrobar Reader is always the easiest way to add a new stamp to a PDF. This algorithm is required by the customer. – lego_game Mar 10 '22 at 05:33
  • Why not just use a PDF manipulation library? Having to integrate with Adobe’s software at runtime just adds a bunch of moving parts… – Dai Mar 10 '22 at 05:34
  • The location of the stamp is not known in advance and the user places it manually after it is generated. – lego_game Mar 10 '22 at 05:38

0 Answers0