C# 将图像元素添加到webbrowser控件页(现有站点)

C# 将图像元素添加到webbrowser控件页(现有站点),c#,forms,C#,Forms,我正在尝试创建一个带有windows窗体webbrowser控件现有站点的图像的元素。然后我想将创建的元素移动到某个位置。我有这个代码,但它不工作 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Document.GetElementById("gpu_notice").Style = "disp

我正在尝试创建一个带有windows窗体webbrowser控件现有站点的图像的元素。然后我想将创建的元素移动到某个位置。我有这个代码,但它不工作

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.GetElementById("gpu_notice").Style = "display:none"; 
        webBrowser1.Document.GetElementById("header").OuterHtml = "";
        webBrowser1.Document.GetElementById("ads").OuterHtml = "";
        webBrowser1.Document.GetElementById("the_game").Style += "position: absolute; top: 0px; right: 0px;";
        HtmlElement logo = webBrowser1.Document.CreateElement("logo");
        logo.SetAttribute("SRC", @"C:\Users\Susana\Documents\Projeto HaxballK\Design\Logo HK.png");
        webBrowser1.Document.Body.AppendChild(logo);
        logo.SetAttribute("float", "right");
    }

向现有HTML文档中插入元素的任务是否使用
WebBrowser
类显示?如果是,则可以执行步骤(抱歉,未测试)

  • 使用
    WebClient
    作为HTML字符串下载文档
  • 在HTML字符串中使
    相对(在
    元素中插入
    style='position:relative'
  • 之后将元素插入HTML字符串,使元素成为绝对元素(设置
    style='position:absolute;left=…;right=…'
  • 将修改后的HTML字符串设置为
    WebBrowser
    类的
    DocumentText
    属性
  • 您必须使用“img”标记而不是“logo”,并使用设置文件路径

    已更新

    如果要在所有JavaScript初始化运行后添加图像,只需使用一个关闭的
    计时器

    System.Windows.Forms.Timer timer = new Timer();
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // other processes
    
        timer.Interval = 1 * 1000 * 5; // 5 seconds
        timer.Tick += timer_Tick;
        timer.Start();
    }
    
    void timer_Tick(object sender, EventArgs e)
    {
        timer.Tick -= timer_Tick;
    
        HtmlElement logo = webBrowser1.Document.CreateElement("img");
        logo.SetAttribute("SRC", @"file:///C:/Users/Susana/Documents/Projeto HaxballK/Design/Logo HK.png");
        webBrowser1.Document.Body.AppendChild(logo);
        logo.SetAttribute("float", "right");
    }
    

    是的,这正是我想要的。我会试试看,当你有时间的时候,你能用这个指令写一个代码吗?谢谢你的回答,我试过了,当页面加载时,图标会出现,但是当页面完全加载时,图像会消失。我不能在这个页面上使用DocumentCompleted事件吗?@Khoury39
    DocumentCompleted
    s应该没问题,这段代码只是添加了
    。我想JavaScript可能会删除你的徽标,所以请尝试我的更新答案。@Khoury39你能发布网站URL吗?当然可以。Haxball.com。这是一个flash游戏,我正在尝试制作一个非浏览器应用程序,它会删除除游戏之外的所有元素。现在我正在尝试在页面上放置一个徽标和一个菜单。。。
    System.Windows.Forms.Timer timer = new Timer();
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // other processes
    
        timer.Interval = 1 * 1000 * 5; // 5 seconds
        timer.Tick += timer_Tick;
        timer.Start();
    }
    
    void timer_Tick(object sender, EventArgs e)
    {
        timer.Tick -= timer_Tick;
    
        HtmlElement logo = webBrowser1.Document.CreateElement("img");
        logo.SetAttribute("SRC", @"file:///C:/Users/Susana/Documents/Projeto HaxballK/Design/Logo HK.png");
        webBrowser1.Document.Body.AppendChild(logo);
        logo.SetAttribute("float", "right");
    }