1
votes

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.

2
What is white space between pixels?EFraim
Would you accomplish the same thing by simply resizing the graphic? Depending on your environment (asp.net and C# or VB I presume by your tags) there are simple functions you can call to end up with a resized bitmap. Perhaps not to your specific expectation but often good enough, depending on the nature of the source image and the sizes you're going to and from.Bob Kaufman
@EFraim - I'd like the spacing to be adjustable... so its really not important. It ought to be enough that its less than the size of a real pixel on the display. @Bob Kaufman - As I mentioned in a comment below, I'm trying to emulate an old looking LCD display which has a low resolution, with spaces between the pixels, so doing a simple resize doesn't cut it, because the resulting pixels "butt up" against each other.Nick

2 Answers

1
votes

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?

0
votes

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.