On-screen Measurement Grid

When shopping for gloves it is really useful to know hand size. Normal person would find a measuring tape. Me? I decided to make a on-screen measurement grid.

First step is just figuring pixel dimensions in relation to my diagonal. A bit of Pythagorean later, the following proved to be what I needed:

var diagonal = 15.6;
var pixelWidth = 1920;
var pixelHeight = 1080;

var ratio = 16.0 / 9;
var height = diagonal / Math.Sqrt(ratio * ratio + 1);
var width = height * ratio; // not really needed

var pixelsPerInch = pixelHeight / height;

var inchWidth = pixelWidth / pixelsPerInch;
var inchHeight = pixelHeight / pixelsPerInch;

Notice here that I am using "witchcraft" units instead of millimetres as I normally would. Reason for that is simple - I was buying gloves on USA site and all measurements were in inches. My screen measurement was also in inches. With both these units being the same, it made no sense to convert into something else first.

Also notice I am only using height to determine pixel density thus making an assumption pixel is a perfect square. Unless you are dealing with something really strange, this assumption is perfectly good.

With these basic calculations done, it's time to draw. Notice I have a few multiplications/divisions by 4 hear - the only reason for these is due to me finding inch-based grid way too coarse. A quarter-inch grid gives a bit more flexibility here.

using (var bmp = new Bitmap(pixelWidth, pixelHeight))
using (var g = Graphics.FromImage(bmp))
g.FillRectangle(Brushes.White, 0, 0, pixelWidth, pixelHeight);

for (var i = 0; i < (int)Math.Ceiling(inchWidth) * 4; i++) {
var pen = (i % 4 == 0) ? Pens.Black : Pens.LightBlue;
var x = (int)(i / 4.0 * pixelsPerInch);
g.DrawLine(pen, x, 0, x, pixelHeight);

for (var i = 0; i < (int)Math.Ceiling(inchHeight) * 4; i++)
var pen = (i % 4 == 0) ? Pens.Black : Pens.LightBlue;
var y = (int)(i / 4.0 * pixelsPerInch);
g.DrawLine(pen, 0, y, pixelWidth, y);

I made this image my background and voila! Now I can measure my hand without ever leaving my chair.

Leave a Reply

Your email address will not be published. Required fields are marked *