473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A One Pixel Long Line

I try to define a line with the length of 1 unit (1 pixel) that is, in fact,
just a point on the screen. But when I draw it with a wide pen with the
appropriate StartCap and EndCap (Round), it will appear as a dot.

I can think of a way to define a 1-pixel long line but it is ugly and
clumsy.
I subtract .5 from the given pixel's X and Y coordinate for the first
System.Drawing.PointF structure, and add .5 to the other PointF stucture. It
seems to work (see the code below), but it doesn't feel good.

Is there a standard way to obtain a line with unit length 1?

//namespace OnePixelLine
namespace OnePixelLine
{
//class Form
class Form:System.Windows.Forms.Form
{
//one integer type point that won't produce output
System.Drawing.Point point0=new System.Drawing.Point(50,50);
//two integer type points that produce 2-pixels long line
System.Drawing.Point point1=new System.Drawing.Point(100,100);
System.Drawing.Point point2=new System.Drawing.Point(101,101);

//two float type points that produce 1-pixel long line, but it is ugly
System.Drawing.PointF pointf1=new System.Drawing.PointF(149.5f,149.5f);
System.Drawing.PointF pointf2=new System.Drawing.PointF(150.5f,150.5f);

//a pen
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Red);

//constructor
Form()
{

//widen pen. Remove this to check the length of the lines
pen.Width=9;
//set StartCap and EndCap both to Round
pen.StartCap=System.Drawing.Drawing2D.LineCap.Roun d;
pen.EndCap=System.Drawing.Drawing2D.LineCap.Round;
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}


//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{

//this outputs nothing
b.Graphics.DrawLine(pen,point0,point0);
//this outputs a line 2 pixels long
b.Graphics.DrawLine(pen,point1,point2);
//this outputs a line 1 pixel long, but it is clumsy
b.Graphics.DrawLine(pen,pointf1,pointf2);
}

//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}


Apr 17 '06 #1
13 2349
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
I try to define a line with the length of 1 unit (1 pixel) that is, in
fact,
just a point on the screen. But when I draw it with a wide pen with the
appropriate StartCap and EndCap (Round), it will appear as a dot.

I can think of a way to define a 1-pixel long line but it is ugly and
clumsy.
I subtract .5 from the given pixel's X and Y coordinate for the first
System.Drawing.PointF structure, and add .5 to the other PointF stucture.
It
seems to work (see the code below), but it doesn't feel good.

Is there a standard way to obtain a line with unit length 1?
Can't you just draw a pixel?
//namespace OnePixelLine
namespace OnePixelLine
{
//class Form
class Form:System.Windows.Forms.Form
{
//one integer type point that won't produce output
System.Drawing.Point point0=new System.Drawing.Point(50,50);
//two integer type points that produce 2-pixels long line
System.Drawing.Point point1=new System.Drawing.Point(100,100);
System.Drawing.Point point2=new System.Drawing.Point(101,101);

//two float type points that produce 1-pixel long line, but it is ugly
System.Drawing.PointF pointf1=new System.Drawing.PointF(149.5f,149.5f);
System.Drawing.PointF pointf2=new System.Drawing.PointF(150.5f,150.5f);

//a pen
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Red);

//constructor
Form()
{

//widen pen. Remove this to check the length of the lines
pen.Width=9;
//set StartCap and EndCap both to Round
pen.StartCap=System.Drawing.Drawing2D.LineCap.Roun d;
pen.EndCap=System.Drawing.Drawing2D.LineCap.Round;
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}


//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{

//this outputs nothing
b.Graphics.DrawLine(pen,point0,point0);
//this outputs a line 2 pixels long
b.Graphics.DrawLine(pen,point1,point2);
//this outputs a line 1 pixel long, but it is clumsy
b.Graphics.DrawLine(pen,pointf1,pointf2);
}

//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}

Apr 18 '06 #2
<snip>
Is there a standard way to obtain a line with unit length 1?
Can't you just draw a pixel?


No, I need it as a line to do hit testing on it.

Or do you know a way to
1. to do a hit test on a pixel? (with a wide pen)
2. to draw an individual pixel?
Apr 18 '06 #3
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
<snip>
Is there a standard way to obtain a line with unit length 1?
Can't you just draw a pixel?


No, I need it as a line to do hit testing on it.

Or do you know a way to
1. to do a hit test on a pixel? (with a wide pen)
2. to draw an individual pixel?


What do you mean by hittest? Is this a function in the framework or
something you've written?

Apr 18 '06 #4
<snip>
What do you mean by hittest? Is this a function in the framework or
something you've written?


With a hittest I mean finding out if the mouse cursor is over a given area
on the screen. It is within the framework, thanks heaven. Take a look at the
post called 'RE: Hit Detection on a Line' from 18-4-2006 8:28
Apr 18 '06 #5
>> What do you mean by hittest? Is this a function in the framework or
something you've written?


With a hittest I mean finding out if the mouse cursor is over a given area
on the screen. It is within the framework, thanks heaven. Take a look at
the post called 'RE: Hit Detection on a Line' from 18-4-2006 8:28

Sorry, that is supposed to be 17-3-2006 17:03
Apr 18 '06 #6

"Martijn Mulder" <i@m> schreef in bericht
news:44***********************@news.wanadoo.nl...
What do you mean by hittest? Is this a function in the framework or
something you've written?


With a hittest I mean finding out if the mouse cursor is over a given
area on the screen. It is within the framework, thanks heaven. Take a
look at the post called 'RE: Hit Detection on a Line' from 18-4-2006 8:28

Sorry, that is supposed to be 17-3-2006 17:03

Aaarggh 17-4-2006 17:03
Apr 18 '06 #7
>> Can't you just draw a pixel?
No, I need it as a line to do hit testing on it.
Or do you know a way to
1. do a hit test on a pixel?
2. draw an individual pixel?


The individual pixel I want to 'hit' is not part of a line, as I initially
thought, but the sole inhabitant of a System.Drawing.Rectangle object, as in
the code below. See if you are man (or woman) enough to hit the pixel with
the mouse pointer :-)

//namespace HitPixel
namespace HitPixel
{
//class Form
class Form:System.Windows.Forms.Form
{
//the pixel that we want to hit, the 'hitpixel'
System.Drawing.Point point=new System.Drawing.Point(150,150);
//line 1, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point1=new System.Drawing.Point(150,50);
System.Drawing.Point point2=new System.Drawing.Point(150,140);
//line 2, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point3=new System.Drawing.Point(150,250);
System.Drawing.Point point4=new System.Drawing.Point(150,160);
//line 3, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point5=new System.Drawing.Point(50,150);
System.Drawing.Point point6=new System.Drawing.Point(140,150);
//line 4, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point7=new System.Drawing.Point(250,150);
System.Drawing.Point point8=new System.Drawing.Point(160,150);
//the graphicspath. Holds nothing more than the 'hitpixel'
System.Drawing.Drawing2D.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();
//a Black pen
System.Drawing.Pen blackpen=new
System.Drawing.Pen(System.Drawing.Color.Black,20);
//a Red pen
System.Drawing.Pen redpen=new
System.Drawing.Pen(System.Drawing.Color.Red,20);
//a pen
System.Drawing.Pen pen;

//constructor
Form()
{

//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);
//set EndCaps of both pens to ArrowAnchor
blackpen.EndCap=System.Drawing.Drawing2D.LineCap.A rrowAnchor;
redpen.EndCap=System.Drawing.Drawing2D.LineCap.Arr owAnchor;
//set pen
pen=blackpen;
//add Rectangle to graphicspath. Rectangle is 1x1 pixel
graphicspath.AddRectangle
(
new System.Drawing.Rectangle
(
point.X,
point.Y,
1,
1
)
);
//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove );
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}

//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{

//use Black pen
pen=blackpen;

if(graphicspath.IsOutlineVisible(b.X,b.Y,System.Dr awing.Pens.Black))
{

//if pixel is hit, use Red pen
pen=redpen;
}

//force redraw
Invalidate();
}

//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(pen,point1,point2);
b.Graphics.DrawLine(pen,point3,point4);
b.Graphics.DrawLine(pen,point5,point6);
b.Graphics.DrawLine(pen,point7,point8);
b.Graphics.FillPath(System.Drawing.Brushes.Black,g raphicspath);
}

//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}


Apr 18 '06 #8
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
Can't you just draw a pixel?

No, I need it as a line to do hit testing on it.
Or do you know a way to
1. do a hit test on a pixel?
2. draw an individual pixel?


I didn't realise drawing a single pixel wasn't in the framework. You could
use the SetPixel and GetPixel API.

[DllImport("gdi32")]
private static extern int SetPixel(int hdc, int x, int y, int crColor);
[DllImport("gdi32")]
private static extern int GetPixel(int hdc, int x, int y);

I'm still a bit confused about what you want to do. Do you want to make the
pixel larger so it's easier to hit?

Michael
Apr 18 '06 #9
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);


Ever heard of the using statement? :-)

Michael
Apr 18 '06 #10
>> SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);


Ever heard of the using statement? :-)


The using statement? Not using it makes it easier to look up different
classes since I know where they reside. I am an ardent typer :-)
Apr 18 '06 #11
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);


Ever heard of the using statement? :-)


The using statement? Not using it makes it easier to look up different
classes since I know where they reside. I am an ardent typer :-)


It would make everything else harder though, typing, reading, maintaining.
:-)

Michael
Apr 18 '06 #12
>>>> SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWm Paint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);

Ever heard of the using statement? :-)


The using statement? Not using it makes it easier to look up different
classes since I know where they reside. I am an ardent typer :-)


It would make everything else harder though, typing, reading, maintaining.
:-)

I don't agree.

- Typing: My editor makes it very easy to copy a line. All editors do
- Reading: When a long name shows up (i.e. System.Drawing...,
System.Windows.Forms...) I know it is SDK-defined, all else is user defined.
The eye is wandering for repitition, for redundancy
- Maintaining: Maintaining code is a nightmare, any kind of code

Never using using (no, never!) makes the code self-documenting, so to speak.
Only thing is that I am new to C# so I start out typing javax.swing... when
I mean System.Drawing...
Apr 19 '06 #13
"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
I don't agree.

- Typing: My editor makes it very easy to copy a line. All editors do
You can't disagree there. You type more than me, full stop.
- Reading: When a long name shows up (i.e. System.Drawing...,
System.Windows.Forms...) I know it is SDK-defined, all else is user
defined. The eye is wandering for repitition, for redundancy
You read more also. Personally I found your code much more difficult to
read. I know Point is in System.Drawing so don't need to be reminded over
and over. Besides, where it is isn't so important to me that I need see it
every time it is used.
- Maintaining: Maintaining code is a nightmare, any kind of code
That's a cop out. Some code is easier to maintain than other.
Never using using (no, never!) makes the code self-documenting, so to
speak. Only thing is that I am new to C# so I start out typing
javax.swing... when I mean System.Drawing...


I doubt you'll find many developers who agree :-)

Michael
Apr 19 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rolf Brauser | last post by:
Hello, is it possible to draw a one pixel line around every cell in a table? with "border: 1px solid; " just the outer tableborder ist concerned ? thanks for any help rolf
10
by: unknown | last post by:
some one can give me a portable source codes that color a pixel? (env *nix) -- unknown
7
by: TomHL | last post by:
hello, - I have a Bitmap Object named: bmp1 - I already have the numerical values of:red,blue and green colors + I have the X and Y cordinates. How do I set the pixel value via safe code on...
9
by: Rob T | last post by:
This seems like a really stupid question, but here it goes........ is there a command to draw a single pixel? The closest I can get is to draw a line that is 2 pixels wide, or do create...
7
by: walter.alex | last post by:
November 16 2005 -- The recent launch of the new 5 year advertising solution from MillionPixelClick.com means the pixel advertising craze will continue unabated. Selling advertising space by the...
30
by: Chaos | last post by:
As my first attempt to loop through every pixel of an image, I used for thisY in range(0, thisHeight): for thisX in range(0, thisWidth): #Actions here for Pixel thisX, thisY But it takes...
18
by: sadegh | last post by:
I am using VC++6. I want to draw some simple shapes (e.g. line, circle, pixel) on a dialog box. How can I do this? Thanks a lot
10
by: marss | last post by:
<hr style="height:1px"/does not fit because although a line looks like it has 1 pixel with but there are empty spaces above and below line. It can be seen if place two HR elements beside. <hr...
8
by: ofiras | last post by:
Hi, I made a "Paint" program, but I couldn't find a method to paint 1 pixel using graphic state ("Graphics g = Graphics.FromHwnd(this.Handle);") How can I paint 1 pixel? I guess I can make a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.