Getting pixel color off CEF window handle

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

Getting pixel color off CEF window handle

Postby 5thYL » Wed Aug 25, 2021 10:08 am

Hi,
I'm trying to implement a feature that can capture the color of a pixel in cef, but when I hook onto the render widget Chrome_RenderWidgetHostHWND and run this:
Code: Select all
    static public System.Drawing.Color GetPixelColor(IntPtr hwnd, int x, int y)
    {
        IntPtr hdc = GetDC(hwnd);
        uint pixel = GetPixel(hdc, x, y);
        System.Drawing.Color color = System.Drawing.Color.FromArgb(
            (Byte)(pixel),
            (Byte)(pixel >> 8),
            (Byte)(pixel >> 16));
        ReleaseDC(hwnd, hdc);
        return color;
    }


Everything within the CEF window bond is reporting 0,0,0, while everything outside of CEF window bond is 255,255,255

Same results go with Chrome_WidgetWin_0 or CefBrowserWindow handles.

So far the only successful handle of getting pixel color has been WindowsForms10.Window.8.app.0.3d7e188_r6_ad1.
But that simply returns the desktop pixel color instead of color of the specific CEF handle.
So if CEF is covered, the code would report the covering pixel's color than CEF's pixel color.

This is the CEF app I'm working off with: https://github.com/dghkd/FlowerMaster
This is the xaml implementation:
Code: Select all
                        <cefSharp:ChromiumWebBrowser x:Name="mainWeb"
                                                     Width="1145"
                                                     Height="650"
                                                     Margin="0,0,0,0"
                                                     AllowDrop="False"
                                                     FrameLoadEnd="mainWeb_FrameLoadEnd"
                                                     IsBrowserInitializedChanged="mainWeb_IsBrowserInitializedChanged">
                        </cefSharp:ChromiumWebBrowser>


This is the initialization:
Code: Select all
        public static void CefInitialize()
        {
            if (CefCachePath == null
                || CefCachePath == "")
            {
                CefCachePath = Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
               "FlowerMaster",
               "Chromium");
            }

            var cefSettings = new CefSettings()
            {
                CachePath = CefCachePath
            };

            cefSettings.CefCommandLineArgs.Add("proxy-server", GetLocalProxySettingString());
            CefSharpSettings.SubprocessExitIfParentProcessClosed = true;

            Cef.EnableHighDPISupport();
            Cef.Initialize(cefSettings);
        }


I am not completely familiar with CEF, so please let me know if more information is required.

What I am looking for:
A way to get pixel color of CEF, even when the CEF window is covered by another application

Any ideas are appreciated, an alternate way of getting color, another CEF implementation, or some CEF fumbling that can get it working would all help with this!

Thank you,
David.
5thYL
Newbie
 
Posts: 3
Joined: Wed Aug 25, 2021 9:54 am

Re: Getting pixel color off CEF window handle

Postby 5thYL » Wed Aug 25, 2021 6:19 pm

Additional info per Alex's question on Gitter:

The application is definitely using the winform nuget package, here is beginning part of the XAML code: [The source code is on the github page, just posting partial so I'm not taking too much space.
Code: Select all
<Controls:MetroWindow x:Class="FlowerMaster.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                      xmlns:local="clr-namespace:FlowerMaster"
                      mc:Ignorable="d"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      xmlns:cefSharp="clr-namespace:CefSharp.WinForms;assembly=CefSharp.WinForms"


and here is the beginning part of CEF been implemented, note the using CEFsharp.winforms:
Code: Select all
using CefSharp;
using CefSharp.WinForms;
using FlowerMaster.Models;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FlowerMaster.Helpers
{
    public class CefSharpHelper
    {
        #region Win32 API

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);

        #endregion Win32 API

        private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);

        #region Public Member

        /// <summary>
        /// 取得或設定CEF暫存資料夾路徑
        /// </summary>
        public static string CefCachePath { get; set; }

        #endregion Public Member

        #region Public Method

        /// <summary>
        /// CEF全域設定初始化
        /// <para>注意:只能初始化一次,且必須於ChromiumWebBrowser宣告建立實體之前進行初始化</para>
        /// </summary>
        public static void CefInitialize()
        {
            if (CefCachePath == null
                || CefCachePath == "")
            {
                CefCachePath = Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
               "FlowerMaster",
               "Chromium");
            }

            var cefSettings = new CefSettings()
            {
                CachePath = CefCachePath
            };

            cefSettings.CefCommandLineArgs.Add("proxy-server", GetLocalProxySettingString());
            CefSharpSettings.SubprocessExitIfParentProcessClosed = true;

            Cef.EnableHighDPISupport();
            Cef.Initialize(cefSettings);
        }


and this is the references of the mainwindow xaml.cs
From here it seems like it is a WPF application:

Code: Select all
using FlowerMaster.Helpers;
using FlowerMaster.Models;
using FlowerMaster.ViewModel;
using CefSharp;
using MahApps.Metro.Controls.Dialogs;
using Nekoxy;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;


So to conclude, it seems like the app is implemented as a wpf app with winform cefsharp.

Is this where the issue is? Should I switch the references to a wpf cefsharp?

Thank you,
David
5thYL
Newbie
 
Posts: 3
Joined: Wed Aug 25, 2021 9:54 am

Re: Getting pixel color off CEF window handle

Postby amaitland » Wed Aug 25, 2021 6:44 pm

The WPF version renders every frame as a bitmap, you can easily access the Bitmap.

https://github.com/cefsharp/CefSharp/bl ... c.xaml#L12

Performance will be slower.

For the WinForms version there is a related discussion at https://github.com/cefsharp/CefSharp/is ... -372103490

You can take a screenshot using DevTools protocol under WinForms.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: Getting pixel color off CEF window handle

Postby 5thYL » Thu Aug 26, 2021 9:22 am

Hi Alex,

Using the workaround found in https://github.com/cefsharp/CefSharp/issues/2286 worked
settings.CefCommandLineArgs.Add("in-process-gpu", "1");
settings.CefCommandLineArgs.Add("enable-gpu", "1");
settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling", "1");

Thank you SO MUCH!.
David.
5thYL
Newbie
 
Posts: 3
Joined: Wed Aug 25, 2021 9:54 am


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 14 guests