Effetto Lag CefSharp.OffScreen

Stato
Discussione chiusa ad ulteriori risposte.

OttoOtti

Nuovo Utente
83
7
Salve ragazzi ho realizzato un programma che fa un screen del browser e lo salva e che poi viene visualizzato il contenuto in una picture box che ha 3 eventi per simulare i click nel browser nascosto ma quando vedo il risultato il movimento che faccio risulta laggoso e aumenta la ram quale può essere il problema ?
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using CefSharp;
using CefSharp.OffScreen;
using CefSharp.WinForms;
using System.Reflection;
using System.Windows;
using GUI;
using System.IO;

namespace VirtualDesktop
{
    public partial class Form1 : Form
    {
        public CefSharp.OffScreen.ChromiumWebBrowser chromeBrowser2;

        Point cordinate = new Point(0, 0);

        public Form1()
        {
            InitializeComponent();
            Cursor.Hide();
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            // Start the browser after initialize global component
            InitializeChromium();
            timer1.Start();
        }

        public void InitializeChromium()
        {
            CefSharp.WinForms.CefSettings settings = new CefSharp.WinForms.CefSettings();
            settings.WindowlessRenderingEnabled = true;
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component

            //Cef.Initialize(settings2);
            // Create a browser component
            //chromeBrowser2 = new CefSharp.OffScreen.ChromiumWebBrowser("https://www.youtube-nocookie.com/embed/Wshc79waUN4");
            chromeBrowser2 = new CefSharp.OffScreen.ChromiumWebBrowser("https://jspaint.app/");
            //chromeBrowser2 = new CefSharp.OffScreen.ChromiumWebBrowser("D:/GUI/Page1/index.html");
            //chromeBrowser2.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        private void save(string path, Bitmap bmp)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    bmp.Save(memory, ImageFormat.Png);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            Bitmap bmp1 = null;
            await chromeBrowser2.ScreenshotAsync().ContinueWith(
                async (task) =>
                {
                    bmp1 = task.Result;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                });
            if (bmp1 == null)
            {
                return;
            }
            
            

            if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
            pictureBox3.Image = bmp1.Clone(
                new Rectangle(0, 0, bmp1.Width, bmp1.Height),
                System.Drawing.Imaging.PixelFormat.DontCare);
            save("D:/Memory/_Temp_/VirtualDesktop/Finale.png", bmp1);
            bmp1.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click(sender, e);
 
        }
        CefSharp.MouseEvent mouseEventt;
        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            

            pictureBox2.Location = new Point(System.Windows.Forms.Cursor.Position.X + 1, System.Windows.Forms.Cursor.Position.Y + 1);
            mouseEventt = new MouseEvent(e.X, e.Y, CefEventFlags.None);
            chromeBrowser2.GetBrowser().GetHost().SendMouseMoveEvent(mouseEventt, false);

        }


        bool test = false;
        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left && test == false)
            {
                test = true;
                chromeBrowser2.GetBrowser().GetHost().SendMouseClickEvent(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, MouseButtonType.Left, false, 1, CefEventFlags.LeftMouseButton);
                System.Threading.Thread.Sleep(25);
            }

            
            if(e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                test = true;
                chromeBrowser2.GetBrowser().GetHost().SendMouseClickEvent(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, MouseButtonType.Left, false, 2, CefEventFlags.LeftMouseButton);
                System.Threading.Thread.Sleep(25);
            }

        }

        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            if ( test == true)
            {
                test = false;
                chromeBrowser2.GetBrowser().GetHost().SendMouseClickEvent(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, MouseButtonType.Left, true, 1, CefEventFlags.LeftMouseButton);
                System.Threading.Thread.Sleep(25);
            }
        }

        // END CLASS
    }
 

Andretti60

Utente Èlite
6,440
5,091
Non mi stupisce che sia lento, usi un timer che continua a mandare eventi e ad ogni evento fai uno screenshot e fai partire un nuovo Task. Stai andando corto di risorse di sistema.
 
Stato
Discussione chiusa ad ulteriori risposte.

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!