Based on meceks answer, I use a version of the following with great results to capture the webdriver image.
It creates a base64 jpeg string at 90% quality. To avoid pixelation issues, i draw the image onto a canvas which is larger than what i will be presenting the image on later. The image is therefore up-scaled to best fit a box of 600 pixels while preserving aspect ratios.
Since jpeg doesn't support transparency i clear the context with a white background.
var base64string = (driver as IJavaScriptExecutor).ExecuteScript(@"
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
function getMaxSize(srcWidth, srcHeight, maxWidth, maxHeight) {
var widthScale = null;
var heightScale = null;
if (maxWidth != null)
{
widthScale = maxWidth / srcWidth;
}
if (maxHeight != null)
{
heightScale = maxHeight / srcHeight;
}
var ratio = Math.min(widthScale || heightScale, heightScale || widthScale);
return {
width: Math.round(srcWidth * ratio),
height: Math.round(srcHeight * ratio)
};
}
function getBase64FromImage(img, width, height) {
var size = getMaxSize(width, height, 600, 600)
canvas.width = size.width;
canvas.height = size.height;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, size.width, size.height);
ctx.drawImage(img, 0, 0, size.width, size.height);
return canvas.toDataURL('image/jpeg', 0.9);
}
var img = document.querySelector('#foo');
return getBase64FromImage(img, img.width, img.height);
") as string;
var base64 = base64string.Split(',').Last();