selenium C#、Internet Explorer 与 aForge 使用的 Chrome 的屏幕截图,

Screen capture by selenium C#, Internet Explorer vs. Chrome to use by aForge,

public static void ScreenShotAndSave(driver, string FileName)
{
        string userPath = "thePath//image.bmp"
        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile(userPath, ImageFormat.Bmp);
}

我使用上面的代码在任何浏览器中捕获屏幕截图。我使用捕获的图像作为来源。我有一个以 bmp 格式 24bppRgb 格式保存的模板图像。您会注意到,aForge 仅比较 24 或 8 bpp 图像。但是,运行IE测试时文件保存为Format32bppArgb,无法在aForge使用。我很乐意听到您对我的问题的建议。请随时问我更多问题。 提前致谢。

我使用此函数删除带有 Selenium 的 alpha 通道:

public static Bitmap RemoveAlphaChannel(Bitmap bitmapSrc) {
    Rectangle rect = new Rectangle(0, 0, bitmapSrc.Width, bitmapSrc.Height);
    Bitmap bitmapDest = (Bitmap)new Bitmap(bitmapSrc.Width, bitmapSrc.Height, PixelFormat.Format24bppRgb);
    BitmapData dataSrc = bitmapSrc.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    BitmapData dataDest = bitmapDest.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    NativeMethods.CopyMemory(dataDest.Scan0, dataSrc.Scan0, (uint)dataSrc.Stride * (uint)dataSrc.Height);
    bitmapSrc.UnlockBits(dataSrc);
    bitmapDest.UnlockBits(dataDest);
    return bitmapDest;
}

static class NativeMethods {

    const string KERNEL32 = "Kernel32.dll";

    [DllImport(KERNEL32)]
    public extern static void CopyMemory(IntPtr dest, IntPtr src, uint length);

}

这是一个使用 Selenium 的例子:

var screenshot = driver.GetScreenshot();
using(var img = (Bitmap)Bitmap.FromStream(new MemoryStream(screenshot.AsByteArray), false, false)){
    RemoveAlphaChannel(img).Save("abcd.png", ImageFormat.Png);
}