2

We want to show a webpage in a chromium based browser within a wpf application. The website that is displayed within the browser should also be shown on another screen but without interaction. I want to combine the cefsharp wpf browser control and the cefsharp offscreen rendering.

Can I use one chromium instance for displaying the page with interactions in wpf and export the current visible website as an image?

Thank you and best regards,

Simon

James Westgate
  • 11,306
  • 8
  • 61
  • 68
Simon
  • 87
  • 5

1 Answers1

5

Thank you amatiland, it indeed works with the OnPaint Method or Event.

    public MainWindow()
    {
        InitializeComponent();

        Browser.Paint += Browser_Paint;
    }

    void Browser_Paint(object sender, CefSharp.Wpf.PaintEventArgs e)
    {
        Bitmap newBitmap = new Bitmap(e.Width, e.Height, 4 * e.Width, System.Drawing.Imaging.PixelFormat.Format32bppRgb, e.Buffer);

        var aPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "TestImageCefSharpQuant.png");
        newBitmap.Save(aPath);
    }

XAML

    <wpf:ChromiumWebBrowser x:Name="Browser" Address="www.google.com"></wpf:ChromiumWebBrowser>
Simon
  • 87
  • 5
  • Just as a quick follow up if the `Bitmap` contains any sort of transparency then you'd need to use `PixelFormat.Format32bppPArgb` – amaitland Aug 23 '19 at 01:52
  • @amaitland do you have an example with PixelFormat.Format24bppRgb? When i just change the Format the Bitmap is grayscales and the bitmap size properties not the same as on screen – Kevin V Nov 17 '22 at 19:35
  • 1
    @KevinV Look in the project source on GitHub. The OffScreen implementation uses PixelFormat.Format24bppRgb internally. – amaitland Nov 19 '22 at 20:14