gpt4 book ai didi

c# - 如何获取 WatiN 图像元素的位图?

转载 作者:太空狗 更新时间:2023-10-29 20:55:33 26 4
gpt4 key购买 nike

我处理了一些文本字段和其他元素,但我想获取位图以便将其保存在磁盘上的某个位置。如果可能的话,我需要直接从 WatiN 进行操作。

我该怎么做?

最佳答案

我前段时间遇到过类似的问题。Watin 不能直接执行此操作,但它公开了获得一些结果所需的 mshtml 对象。

当时我的代码大概是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using mshtml;
using System.Windows.Forms;

namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Browser browser = new IE("http://www.google.com");
IEElement banner = browser.Images[0].NativeElement as IEElement;

IHTMLElement bannerHtmlElem = banner.AsHtmlElement;
IEElement bodyNative = browser.Body.NativeElement as IEElement;
mshtml.IHTMLElement2 bodyHtmlElem = (mshtml.IHTMLElement2)bodyNative.AsHtmlElement;
mshtml.IHTMLControlRange controlRange = (mshtml.IHTMLControlRange)bodyHtmlElem.createControlRange();

controlRange.add((mshtml.IHTMLControlElement)bannerHtmlElem);
controlRange.execCommand("Copy", false, System.Reflection.Missing.Value);
controlRange.remove(0);

if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
{
System.Drawing.Image image = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
// do something here
}
}
}

}
}

基本上,这个小黑客尝试将图像复制到剪贴板。但是,我在使其正常工作时遇到了一些问题,最终对图像周围的区域进行了快照并将其保存到磁盘。

虽然这可能不是很有帮助,但它可能会为您指明一些方向..

关于c# - 如何获取 WatiN 图像元素的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com