 |
|
| .NET DotNet Forum Index » Visual C# Forum » How to draw a one-pixel line??... |
|
Page 1 of 2 Goto page 1, 2 Next |
|
| Author |
Message |
| Loren Pechtel... |
Posted: Tue Nov 03, 2009 11:59 pm |
|
|
|
Guest
|
I'm having trouble with what seems to be a simple task:
I'm trying to draw some vector images on a field of tiles. The tiles
are always of integer sizes and can zoom over any reasonable range of
values.
The images themselves are vector images but they don't simply scale
up, as the tile gets bigger more detail is added. (Yeah, I know I'll
have to handle printing a bit differently.)
The problem I'm having is with images composed entirely of vertical
and horizontal lines, black on a gray field.
The horizontal lines are fine, I get exactly what I want. Vertical
lines aren't working too well, though. A lone vertical line is good
enough--two pixels of dark gray instead of one of black but it looks
close enough I didn't even realize it was wrong until I got digging
into the case that's awful:
I have some stripes, some are black/background/background and others
are black/background.
With antialiasing on the ones with the black every three look fuzzy
and the closer ones just look like a blurry blob.
With it turned off I'm getting a very clear image, albeit very wrong:
The three-step one produces dark gray/dark gray/background and the
two-step one simply produces a box of dark gray.
Everything is being drawn on a bitmap that is then put in a
picturebox. The picturebox is in a panel that fills the form other
than a few controls on the right for manipulating the view.
The behavior would make sense if somehow I was drawing between pixels
on the y axis but all the DrawLines are being fed integer coordinates. |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 3:15 pm |
|
|
|
Guest
|
On Wed, 4 Nov 2009 06:06:43 -0800 (PST), Geoffrey Summerhayes
<sumrnot at (no spam) gmail.com> wrote:
Quote: On Nov 3, 11:59 pm, Loren Pechtel <lorenpech... at (no spam) hotmail.invalid.com
wrote:
I'm having trouble with what seems to be a simple task:
I'm trying to draw some vector images on a field of tiles. The tiles
are always of integer sizes and can zoom over any reasonable range of
values.
The images themselves are vector images but they don't simply scale
up, as the tile gets bigger more detail is added. (Yeah, I know I'll
have to handle printing a bit differently.)
The problem I'm having is with images composed entirely of vertical
and horizontal lines, black on a gray field.
The horizontal lines are fine, I get exactly what I want. Vertical
lines aren't working too well, though. A lone vertical line is good
enough--two pixels of dark gray instead of one of black but it looks
close enough I didn't even realize it was wrong until I got digging
into the case that's awful:
I have some stripes, some are black/background/background and others
are black/background.
With antialiasing on the ones with the black every three look fuzzy
and the closer ones just look like a blurry blob.
With it turned off I'm getting a very clear image, albeit very wrong:
The three-step one produces dark gray/dark gray/background and the
two-step one simply produces a box of dark gray.
Everything is being drawn on a bitmap that is then put in a
picturebox. The picturebox is in a panel that fills the form other
than a few controls on the right for manipulating the view.
The behavior would make sense if somehow I was drawing between pixels
on the y axis but all the DrawLines are being fed integer coordinates.
My psychic powers must be off today, everything is blurry. Can't see
the code, the size of the bitmap, the size of the picturebox,
properties
of the picturebox, the comments in the code, in fact, even the code!
I was hoping someone could point me in the right direction without
having to distill down an example. While I've been programming a long
time I'm new to C# so I figure I'm probably making an elementary
mistake.
Quote:
On a guess, try making the window 1 pixel wider, see if anything
changes.
No effect. |
|
|
| Back to top |
|
|
|
| Peter Duniho... |
Posted: Wed Nov 04, 2009 3:30 pm |
|
|
|
Guest
|
Loren Pechtel wrote:
Quote: I agree it sounds exactly like fractional coordinates. How do you
draw on fractional coordinates with integer variables, though? [...]
By drawing in an environment where coordinates given as integers are
scaling somehow.
I posted a code example that shows that in general, the problem you
describe does not occur. Either your own code does something different,
or you are simply not observing the results correctly (i.e. you think
there's a problem when there's not).
In the latter case, obviously there's nothing to fix. In the former
case, until you post a concise-but-complete code example that reliably
demonstrates the issue, there's no way to tell you how to fix your code.
Pete |
|
|
| Back to top |
|
|
|
| Jeff Johnson... |
Posted: Wed Nov 04, 2009 5:41 pm |
|
|
|
Guest
|
"Loren Pechtel" <lorenpechtel at (no spam) hotmail.invalid.com> wrote in message
news:%23edaYuQXKHA.1280 at (no spam) TK2MSFTNGP04.phx.gbl...
Quote: I'm having trouble with what seems to be a simple task:
I'm trying to draw some vector images on a field of tiles. The tiles
are always of integer sizes and can zoom over any reasonable range of
values.
The images themselves are vector images but they don't simply scale
up, as the tile gets bigger more detail is added. (Yeah, I know I'll
have to handle printing a bit differently.)
The problem I'm having is with images composed entirely of vertical
and horizontal lines, black on a gray field.
The horizontal lines are fine, I get exactly what I want. Vertical
lines aren't working too well, though. A lone vertical line is good
enough--two pixels of dark gray instead of one of black but it looks
close enough I didn't even realize it was wrong until I got digging
into the case that's awful:
I have some stripes, some are black/background/background and others
are black/background.
With antialiasing on the ones with the black every three look fuzzy
and the closer ones just look like a blurry blob.
With it turned off I'm getting a very clear image, albeit very wrong:
The three-step one produces dark gray/dark gray/background and the
two-step one simply produces a box of dark gray.
Everything is being drawn on a bitmap that is then put in a
picturebox. The picturebox is in a panel that fills the form other
than a few controls on the right for manipulating the view.
The behavior would make sense if somehow I was drawing between pixels
on the y axis but all the DrawLines are being fed integer coordinates.
I have to ask: are you absolutely positive you're actually getting
different-colored pixels, or does it just LOOK that way. In other words,
have you saved your bitmap to disk and then loaded it into a graphics
application and increased the zoom so that you can see the individual
pixels? |
|
|
| Back to top |
|
|
|
| Peter Duniho... |
Posted: Wed Nov 04, 2009 6:28 pm |
|
|
|
Guest
|
Jeff Johnson wrote:
Quote: I have to ask: are you absolutely positive you're actually getting
different-colored pixels, or does it just LOOK that way. In other words,
have you saved your bitmap to disk and then loaded it into a graphics
application and increased the zoom so that you can see the individual
pixels?
To elaborate on this point: the code example I posted last evening is
specifically designed to illustrate this point. It provides for scaling
the image during presentation, as well as inspecting RGB values for any
specific pixel.
With the example, you can see that scaling the image by (for example)
200%, you get the aliasing behavior described by the OP, but the pixel
values are still exactly as he wants (i.e. 0,0,0 RGB where a black line
is drawn, 128,128,128 on the gray background where it wasn't).
Pete |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 8:17 pm |
|
|
|
Guest
|
On Wed, 04 Nov 2009 12:30:56 -0800, Peter Duniho
<no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
Quote: Loren Pechtel wrote:
I agree it sounds exactly like fractional coordinates. How do you
draw on fractional coordinates with integer variables, though? [...]
By drawing in an environment where coordinates given as integers are
scaling somehow.
I posted a code example that shows that in general, the problem you
describe does not occur. Either your own code does something different,
or you are simply not observing the results correctly (i.e. you think
there's a problem when there's not).
In the latter case, obviously there's nothing to fix. In the former
case, until you post a concise-but-complete code example that reliably
demonstrates the issue, there's no way to tell you how to fix your code.
I managed to reproduce it very easily:
A form containing a panel that contains a picture box. Drawing:
Bitmap Image = new Bitmap(Box.ClientSize.Width
,Box.ClientSize.Height);
Pen Ink = new Pen(Color.Black, -1);
using (Graphics Paper = Graphics.FromImage(Image))
{
Paper.Clear(Color.LightGray);
for (int x = 2; x < Image.Width / 2; x += 2)
Paper.DrawLine(Ink, x, 0, x, Image.Height - 1);
for (int y = 2; y < Image.Height; y += 2)
Paper.DrawLine(Ink, Image.Width / 2, y, Image.Width -
1, y);
}
Box.Image = Image;
Obviously the panel in this case is useless but I simply copied the
layout from the main code--in the main code there are some controls
off to the side and I use the panel to make the picture box take up
the whole form.
This code *SHOULD* draw one half of the screen with vertical lines and
one half with horizontal. I'm getting a gray mass on the left and the
expected behavior on the right. |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 8:17 pm |
|
|
|
Guest
|
On Wed, 4 Nov 2009 17:41:52 -0500, "Jeff Johnson" <i.get at (no spam) enough.spam>
wrote:
Quote: "Loren Pechtel" <lorenpechtel at (no spam) hotmail.invalid.com> wrote in message
news:%23edaYuQXKHA.1280 at (no spam) TK2MSFTNGP04.phx.gbl...
I'm having trouble with what seems to be a simple task:
I'm trying to draw some vector images on a field of tiles. The tiles
are always of integer sizes and can zoom over any reasonable range of
values.
The images themselves are vector images but they don't simply scale
up, as the tile gets bigger more detail is added. (Yeah, I know I'll
have to handle printing a bit differently.)
The problem I'm having is with images composed entirely of vertical
and horizontal lines, black on a gray field.
The horizontal lines are fine, I get exactly what I want. Vertical
lines aren't working too well, though. A lone vertical line is good
enough--two pixels of dark gray instead of one of black but it looks
close enough I didn't even realize it was wrong until I got digging
into the case that's awful:
I have some stripes, some are black/background/background and others
are black/background.
With antialiasing on the ones with the black every three look fuzzy
and the closer ones just look like a blurry blob.
With it turned off I'm getting a very clear image, albeit very wrong:
The three-step one produces dark gray/dark gray/background and the
two-step one simply produces a box of dark gray.
Everything is being drawn on a bitmap that is then put in a
picturebox. The picturebox is in a panel that fills the form other
than a few controls on the right for manipulating the view.
The behavior would make sense if somehow I was drawing between pixels
on the y axis but all the DrawLines are being fed integer coordinates.
I have to ask: are you absolutely positive you're actually getting
different-colored pixels, or does it just LOOK that way. In other words,
have you saved your bitmap to disk and then loaded it into a graphics
application and increased the zoom so that you can see the individual
pixels?
When stripes turn into a blob instead it's quite obvious. While I was
working on features other than stripes I didn't realize anything was
wrong, it was close enough. |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 8:17 pm |
|
|
|
Guest
|
On Wed, 04 Nov 2009 15:28:59 -0800, Peter Duniho
<no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
Quote: Jeff Johnson wrote:
I have to ask: are you absolutely positive you're actually getting
different-colored pixels, or does it just LOOK that way. In other words,
have you saved your bitmap to disk and then loaded it into a graphics
application and increased the zoom so that you can see the individual
pixels?
To elaborate on this point: the code example I posted last evening is
specifically designed to illustrate this point. It provides for scaling
the image during presentation, as well as inspecting RGB values for any
specific pixel.
With the example, you can see that scaling the image by (for example)
200%, you get the aliasing behavior described by the OP, but the pixel
values are still exactly as he wants (i.e. 0,0,0 RGB where a black line
is drawn, 128,128,128 on the gray background where it wasn't).
There should be no scaling in my case--I want to add more detail as
the image gets bigger and thus I'm drawing it at the exact size I want
(or using scroll bars if the minimum acceptable size is bigger than
the window size.) |
|
|
| Back to top |
|
|
|
| Family Tree Mike... |
Posted: Wed Nov 04, 2009 8:40 pm |
|
|
|
Guest
|
Loren Pechtel wrote:
Quote: On Wed, 04 Nov 2009 12:30:56 -0800, Peter Duniho
no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
Loren Pechtel wrote:
I agree it sounds exactly like fractional coordinates. How do you
draw on fractional coordinates with integer variables, though? [...]
By drawing in an environment where coordinates given as integers are
scaling somehow.
I posted a code example that shows that in general, the problem you
describe does not occur. Either your own code does something different,
or you are simply not observing the results correctly (i.e. you think
there's a problem when there's not).
In the latter case, obviously there's nothing to fix. In the former
case, until you post a concise-but-complete code example that reliably
demonstrates the issue, there's no way to tell you how to fix your code.
I managed to reproduce it very easily:
A form containing a panel that contains a picture box. Drawing:
Bitmap Image = new Bitmap(Box.ClientSize.Width
,Box.ClientSize.Height);
Pen Ink = new Pen(Color.Black, -1);
using (Graphics Paper = Graphics.FromImage(Image))
{
Paper.Clear(Color.LightGray);
for (int x = 2; x < Image.Width / 2; x += 2)
Paper.DrawLine(Ink, x, 0, x, Image.Height - 1);
for (int y = 2; y < Image.Height; y += 2)
Paper.DrawLine(Ink, Image.Width / 2, y, Image.Width -
1, y);
}
Box.Image = Image;
Obviously the panel in this case is useless but I simply copied the
layout from the main code--in the main code there are some controls
off to the side and I use the panel to make the picture box take up
the whole form.
This code *SHOULD* draw one half of the screen with vertical lines and
one half with horizontal. I'm getting a gray mass on the left and the
expected behavior on the right.
For what it is worth, I see what you say it *SHOULD* draw when I execute
your routine.
--
Mike |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 9:33 pm |
|
|
|
Guest
|
On Wed, 04 Nov 2009 20:40:56 -0500, Family Tree Mike
<FamilyTreeMike at (no spam) ThisOldHouse.com> wrote:
Quote: Loren Pechtel wrote:
On Wed, 04 Nov 2009 12:30:56 -0800, Peter Duniho
no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
Loren Pechtel wrote:
I agree it sounds exactly like fractional coordinates. How do you
draw on fractional coordinates with integer variables, though? [...]
By drawing in an environment where coordinates given as integers are
scaling somehow.
I posted a code example that shows that in general, the problem you
describe does not occur. Either your own code does something different,
or you are simply not observing the results correctly (i.e. you think
there's a problem when there's not).
In the latter case, obviously there's nothing to fix. In the former
case, until you post a concise-but-complete code example that reliably
demonstrates the issue, there's no way to tell you how to fix your code.
I managed to reproduce it very easily:
A form containing a panel that contains a picture box. Drawing:
Bitmap Image = new Bitmap(Box.ClientSize.Width
,Box.ClientSize.Height);
Pen Ink = new Pen(Color.Black, -1);
using (Graphics Paper = Graphics.FromImage(Image))
{
Paper.Clear(Color.LightGray);
for (int x = 2; x < Image.Width / 2; x += 2)
Paper.DrawLine(Ink, x, 0, x, Image.Height - 1);
for (int y = 2; y < Image.Height; y += 2)
Paper.DrawLine(Ink, Image.Width / 2, y, Image.Width -
1, y);
}
Box.Image = Image;
Obviously the panel in this case is useless but I simply copied the
layout from the main code--in the main code there are some controls
off to the side and I use the panel to make the picture box take up
the whole form.
This code *SHOULD* draw one half of the screen with vertical lines and
one half with horizontal. I'm getting a gray mass on the left and the
expected behavior on the right.
For what it is worth, I see what you say it *SHOULD* draw when I execute
your routine.
What in the world is the difference then? How do I get it to draw
what you're seeing? |
|
|
| Back to top |
|
|
|
| Family Tree Mike... |
Posted: Wed Nov 04, 2009 9:55 pm |
|
|
|
Guest
|
Loren Pechtel wrote:
Quote: On Wed, 04 Nov 2009 20:40:56 -0500, Family Tree Mike
FamilyTreeMike at (no spam) ThisOldHouse.com> wrote:
Loren Pechtel wrote:
On Wed, 04 Nov 2009 12:30:56 -0800, Peter Duniho
no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
<snip>
Quote:
This code *SHOULD* draw one half of the screen with vertical lines and
one half with horizontal. I'm getting a gray mass on the left and the
expected behavior on the right.
For what it is worth, I see what you say it *SHOULD* draw when I execute
your routine.
What in the world is the difference then? How do I get it to draw
what you're seeing?
I suspect screen settings. When I set my display settings really
poorly, then I see what you observe. My normal settings are 1680x1050
32 bit color.
--
Mike |
|
|
| Back to top |
|
|
|
| Peter Duniho... |
Posted: Wed Nov 04, 2009 11:04 pm |
|
|
|
Guest
|
Loren Pechtel wrote:
Quote: What in the world is the difference then? How do I get it to draw
what you're seeing?
You've yet to establish it's _not_ drawing what Mike is seeing.
Visual inspection isn't good enough. Just because you've drawn a bitmap
a certain way, that's no guarantee it will be presented on the screen
that way.
Pete |
|
|
| Back to top |
|
|
|
| Peter Duniho... |
Posted: Wed Nov 04, 2009 11:06 pm |
|
|
|
Guest
|
Loren Pechtel wrote:
Quote: There should be no scaling in my case [...]
So you say. But until you post a concise-but-complete code example,
it's not possible to know for sure there's not. If I had a nickel for
every time someone asked a question, 100% certain that their description
of the situation was accurate, only to find it out wasn't...
And even if we establish there's no scaling in the _drawing_ of the
bitmap, there's still some possibility you've got some non-standard
system configuration that is meddling with the display on-screen.
Pete |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 11:53 pm |
|
|
|
Guest
|
On Wed, 04 Nov 2009 21:55:05 -0500, Family Tree Mike
<FamilyTreeMike at (no spam) ThisOldHouse.com> wrote:
Quote: Loren Pechtel wrote:
On Wed, 04 Nov 2009 20:40:56 -0500, Family Tree Mike
FamilyTreeMike at (no spam) ThisOldHouse.com> wrote:
Loren Pechtel wrote:
On Wed, 04 Nov 2009 12:30:56 -0800, Peter Duniho
no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
snip
This code *SHOULD* draw one half of the screen with vertical lines and
one half with horizontal. I'm getting a gray mass on the left and the
expected behavior on the right.
For what it is worth, I see what you say it *SHOULD* draw when I execute
your routine.
What in the world is the difference then? How do I get it to draw
what you're seeing?
I suspect screen settings. When I set my display settings really
poorly, then I see what you observe. My normal settings are 1680x1050
32 bit color.
3x 1280x1024 at (no spam) 32bit. |
|
|
| Back to top |
|
|
|
| Loren Pechtel... |
Posted: Wed Nov 04, 2009 11:53 pm |
|
|
|
Guest
|
On Wed, 04 Nov 2009 20:04:02 -0800, Peter Duniho
<no.peted.spam at (no spam) no.nwlink.spam.com> wrote:
Quote: Loren Pechtel wrote:
What in the world is the difference then? How do I get it to draw
what you're seeing?
You've yet to establish it's _not_ drawing what Mike is seeing.
Visual inspection isn't good enough. Just because you've drawn a bitmap
a certain way, that's no guarantee it will be presented on the screen
that way.
Confirmed--it's drawn right, rendered wrong. It doesn't take blowing
it up to find, either.
Ok, I've been barking up the wrong tree. What could be causing the
errant display? |
|
|
| Back to top |
|
|
|
|
|
All times are GMT - 5 Hours
The time now is Tue Nov 24, 2009 8:51 pm
|
|