.net – Insert Space Between Pixels


I need to be able to draw a bitmap that is at a specific resolution (~40 DPI), on screen using GDI, and also be able to replicate spacing between each pixel. The space is a fraction of the pixel size, but it is noticeable to the eye.

Is there anyway to setup the Graphics class or a Bitmap to have it insert "white space" between drawn pixels? Before I go after writing the complicated code to do it myself, I'd like to make sure there isn't some setting I'm missing somewhere.

Best Solution

It sounds like you want to stretch an image from 40 dpi up to the ~110 dpi of a monitor, but rather than expanding the pixels, still only draw 40 pixels per inch and have the rest be white.

I don't know of anything in GDI that does this. Also, it'll look really bad unless you do integer scaling - i.e. exactly one or two white pixels for every pixel in the original bitmap, which will severely limit your options for the final size. You might be able to tweak that a bit by using subpixel rendering (e.g. ClearType), but that'll be even more specialized code (and monitor-specific!) you'll have to write. As it stands now, you're probably going to have to construct a new bitmap pixel-by-pixel.

You can get reasonably close by creating a new bitmap that is 3x3 times the size of your source bitmap, painting it white, and (pseudocode)

foreach(point p in oldBitmap)
    // draw a 2x2 box into every 3x3 box
    newbitmap.DrawBox(p.x * 3, p.y * 3, 2, 2, p.Color); 
DrawBitmap(target, newbitmap)

You might want to add some single-pixel adjustments to get a nice white border around the edge.

This still leaves 5/9 of your pixels white, meaning you'll have a very washed-out image.

Are you trying to emulate an old display or something?