I use a Gecko .NET WebBrowser control to get the HTML page.
Then I use this code to make a screenshot of this HTML page:
void LoadingFinished(object sender, EventArgs args)
{
try
{
myBrowser.Document.Body.SetAttribute("style", "overflow:hidden");
if (screenshotkey != "")
{
Bitmap bitmap = new Bitmap(myBrowser.Width, myBrowser.Height);
myBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, myBrowser.Width, myBrowser.Height));
bitmap.Save("Screenshots/" + screenshotkey + ".png", ImageFormat.Png);
}
}
catch (Exception dfkvsn)
{
errorloadingpage();
}
}
I removed some stuff from the code above that is not very important – like setting browser width, etc. Originally the code is 100% functional.
The problem is that when it saves a png screenshot (DrawToBitmap line) – at some places the created picture has the white pixels that should not be there (WebBrowser originally does not have them), see the link.
Same thing was happening when I was using standard .NET WebBrowser control, so it’s not Gecko problem.
I have no idea how to approach this.
Is there a better way to save it to bitmap? Or is this the best .NET can do?
Fixed it.
It turned out that pixels that seemed to be white – were just transparent.
And they were only in places where picture had a deepest black color (RGB 0, 0, 0)
For some reason .NET couldn’t save this color to the file.
So I simply added the line of code that makes entire picture black, and on top of that I record my screenshot.
Since it’s 2 layers now – transparent pixels now seem to be black – just what I wanted – no white pixels.
Here’s the final code: