tags:

views:

105

answers:

2

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.

+1  A: 

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?

Aric TenEyck
Close. I'm trying to emulate an LCD display that has a specific DPI and specific amount of physical space between its pixels. There is enough space between the pixels, that some text which looks bad in the "non spaced" bitmap (because the pixels looked bunched) actually looks OK on the actual display.
Nick
A: 

A Bitmap in .Net doesn't have any logical conception of the space between pixels. I think you'll have to write this yourself. When you do this, make sure you access the data using the Bitmap's LockBits method - SetPixel and GetPixel are unbelievably slow.

Update: I'm so bored today I want to write this for you. If no one else has a good answer, I'll do it after lunch.

MusiGenesis
HA HA... I don't have an aversion to writing it myself... so far in my project I've been doing a A LOT of GDI work... its just I hate writing code myself when a solution already exists. I was just hoping I missed something.
Nick
I could be wrong, but I doubt there's anything built-in that does this. Not much call for emulating old LCD displays. =)
MusiGenesis