MaxBeauchemin
1/3/2018 - 11:44 PM

Bitmap Desktop Screenshot

How to take a screenshot of the desktop and convert it into a bitmap. Will not capture items in Fullscreen mode, i.e. games.

var bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics myGraphics = Graphics.FromImage(bitmap);

// Take the screenshot from the upper left corner to the right bottom corner.
myGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                          Screen.PrimaryScreen.Bounds.Y,
                          0,
                          0,
                          Screen.PrimaryScreen.Bounds.Size);

var offset = 25;

var pixelCount = 0;
var r = 0;
var g = 0;
var b = 0;
for(int x = 0; x < Screen.PrimaryScreen.Bounds.Width; x += offset)
{
    for (int y = 0; y < Screen.PrimaryScreen.Bounds.Height; y += offset)
    {
        var c = bitmap.GetPixel(x, y);

        r += c.R;
        g += c.G;
        b += c.B;
        pixelCount++;
    }
}

var color = Color.FromArgb(r / pixelCount, g / pixelCount, b / pixelCount);

var hue = color.GetHue();
var sat = color.GetSaturation();
var bri = color.GetBrightness();

body.hue = (int)((hue/360)* 65535);
body.sat = (int)(sat*255);
body.bri = (int)(bri*255);