473,756 Members | 3,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Win dows.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,1 49.5f);
System.Drawing. PointF pointf2=new System.Drawing. PointF(150.5f,1 50.5f);

//a pen
System.Drawing. Pen pen=new System.Drawing. Pen(System.Draw ing.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=Sy stem.Drawing.Dr awing2D.LineCap .Round;
pen.EndCap=Syst em.Drawing.Draw ing2D.LineCap.R ound;
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);
}


//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{

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

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


Apr 17 '06 #1
13 2388
"Martijn Mulder" <i@m> wrote in message
news:44******** *************** @news.wanadoo.n l...
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.Win dows.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,1 49.5f);
System.Drawing. PointF pointf2=new System.Drawing. PointF(150.5f,1 50.5f);

//a pen
System.Drawing. Pen pen=new System.Drawing. Pen(System.Draw ing.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=Sy stem.Drawing.Dr awing2D.LineCap .Round;
pen.EndCap=Syst em.Drawing.Draw ing2D.LineCap.R ound;
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);
}


//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{

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

//Main
public static void Main()
{
System.Windows. Forms.Applicati on.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.n l...
<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.n l...
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.Win dows.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.Graph icsPath graphicspath=ne w
System.Drawing. Drawing2D.Graph icsPath();
//a Black pen
System.Drawing. Pen blackpen=new
System.Drawing. Pen(System.Draw ing.Color.Black ,20);
//a Red pen
System.Drawing. Pen redpen=new
System.Drawing. Pen(System.Draw ing.Color.Red,2 0);
//a pen
System.Drawing. Pen pen;

//constructor
Form()
{

//set style for double buffered graphics
SetStyle
(
System.Windows. Forms.ControlSt yles.AllPaintin gInWmPaint|
System.Windows. Forms.ControlSt yles.DoubleBuff er|
System.Windows. Forms.ControlSt yles.ResizeRedr aw|
System.Windows. Forms.ControlSt yles.UserPaint,
true
);
//set EndCaps of both pens to ArrowAnchor
blackpen.EndCap =System.Drawing .Drawing2D.Line Cap.ArrowAnchor ;
redpen.EndCap=S ystem.Drawing.D rawing2D.LineCa p.ArrowAnchor;
//set pen
pen=blackpen;
//add Rectangle to graphicspath. Rectangle is 1x1 pixel
graphicspath.Ad dRectangle
(
new System.Drawing. Rectangle
(
point.X,
point.Y,
1,
1
)
);
//prepare for mouse-input
MouseMove+=new System.Windows. Forms.MouseEven tHandler(OnMous eMove);
//prepare for paint
Paint+=new System.Windows. Forms.PaintEven tHandler(OnPain t);
}

//OnMouseMove
void OnMouseMove(obj ect a,System.Window s.Forms.MouseEv entArgs b)
{

//use Black pen
pen=blackpen;

if(graphicspath .IsOutlineVisib le(b.X,b.Y,Syst em.Drawing.Pens .Black))
{

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

//force redraw
Invalidate();
}

//OnPaint
void OnPaint(object a,System.Window s.Forms.PaintEv entArgs b)
{
b.Graphics.Draw Line(pen,point1 ,point2);
b.Graphics.Draw Line(pen,point3 ,point4);
b.Graphics.Draw Line(pen,point5 ,point6);
b.Graphics.Draw Line(pen,point7 ,point8);
b.Graphics.Fill Path(System.Dra wing.Brushes.Bl ack,graphicspat h);
}

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


Apr 18 '06 #8
"Martijn Mulder" <i@m> wrote in message
news:44******** *************** @news.wanadoo.n l...
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("gdi3 2")]
private static extern int SetPixel(int hdc, int x, int y, int crColor);
[DllImport("gdi3 2")]
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.n l...
SetStyle
(
System.Windows. Forms.ControlSt yles.AllPaintin gInWmPaint|
System.Windows. Forms.ControlSt yles.DoubleBuff er|
System.Windows. Forms.ControlSt yles.ResizeRedr aw|
System.Windows. Forms.ControlSt yles.UserPaint,
true
);


Ever heard of the using statement? :-)

Michael
Apr 18 '06 #10

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

Similar topics

6
3319
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
3109
by: unknown | last post by:
some one can give me a portable source codes that color a pixel? (env *nix) -- unknown
7
3601
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 the bmp1 object? (the pixel location based on the X & Y values and it's color based on the red,blue and green values the I already have)
9
12968
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 another image that's 1x1 with the color of the pixel, then do a DrawImage to insert that image into the 'master' image...which seems like a huge waste! Thanks.
7
1748
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 pixel the new form of advertising is proving popular with both clients and providers alike. Designed to boost traffic, an image is placed on MillionPixelClick.com with a link to your website. These pixels will be visible for at least 5 years....
30
9078
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 450-1000 milliseconds I want speeds less than 10 milliseconds
18
3513
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
6976
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 style="height:1px"/> <hr style="height:1px"/> I tried also <div style="height:1px;font-size:1px;background- color:black;"></divbut a line was 2 pixel thick :(
8
3549
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 Bitmap, change one pixel color and show it, but I'm sure there is another way of doing it. Please help, Ofir.
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.