473,396 Members | 1,846 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,396 software developers and data experts.

How Can I Paint a Pixel?

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.
Aug 5 '08 #1
8 3528
On Aug 5, 12:11*pm, ofiras <ofi...@gmail.comwrote:
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.
There isn't. GDI+ is a resolution-independent drawing framework, and
the underlying device may not even be a raster device, in theory (GDI+
plotter, anyone?). So painting a single-pixel bitmap is about the only
workaround (there's no way to draw a 1-pixel-long line or rectangle
reliably).

Well, or you can use plain GDI via P/Invoke - that has SetPixel().
Aug 5 '08 #2
On 5 אוגוסט, 10:24, Pavel Minaev <int...@gmail.comwrote:
On Aug 5, 12:11*pm, ofiras <ofi...@gmail.comwrote:
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.

There isn't. GDI+ is a resolution-independent drawing framework, and
the underlying device may not even be a raster device, in theory (GDI+
plotter, anyone?). So painting a single-pixel bitmap is about the only
workaround (there's no way to draw a 1-pixel-long line or rectangle
reliably).

Well, or you can use plain GDI via P/Invoke - that has SetPixel().
Thanks... I'll use Bitmap to paint pixel...
One more question - How can I save graphics that the user has drawn to
Bitmap?
Or how can I draw line/circle in Bitmap (so every time the user wii
draw a line or a circle I can do it in the Bitmap too)?
Please help,
Ofir.
Aug 5 '08 #3
ofiras wrote:
How can I save graphics that the user has drawn to
Bitmap?
Well, how do you keep the graphics so that you can repaint it on the screen?
Or how can I draw line/circle in Bitmap (so every time the user wii
draw a line or a circle I can do it in the Bitmap too)?
You use the Graphics.FromImage method to create a Graphics object that
you can use to draw on the Bitmap.

--
Göran Andersson
_____
http://www.guffa.com
Aug 5 '08 #4
The System.Drawing.Bitmap class has a SetPixel and GetPixel method.
"ofiras" <of****@gmail.comwrote in message
news:e5**********************************@c58g2000 hsc.googlegroups.com...
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.
Aug 5 '08 #5
On 5 אוגוסט, 11:30, Göran Andersson <gu....@guffa.comwrote:
ofiras wrote:
How can I save graphics that the user has drawn to
Bitmap?

Well, how do you keep the graphics so that you can repaint it on the screen?
Or how can I draw line/circle in Bitmap (so every time the user wii
draw a line or a circle I can do it in the Bitmap too)?

You use the Graphics.FromImage method to create a Graphics object that
you can use to draw on the Bitmap.

--
Göran Andersson
_____http://www.guffa.com
Thanks, it helped a lot.
Another question - in bitmap, how can I find the nearest pixel (pixel
1) to a specific pixel (pixel 2) that has different color from pixel
2? Or how can I find a pixel in a specific distance from pixel 2 (like
a circle that pixel 2 is his center, and I'm looking for a pixel that
has different color that pixel 2)?
(I'm trying to do a voronoi diagram maker, so I need to search for the
nearest colored pixel in my bitmap for every pixel that is not colored
(white))

And one more question - how can I load picture and change its width
and height?

Aug 5 '08 #6
On Tue, 05 Aug 2008 01:24:51 -0700, Pavel Minaev <in****@gmail.comwrote:
[...]
>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.

There isn't. GDI+ is a resolution-independent drawing framework, and
the underlying device may not even be a raster device, in theory (GDI+
plotter, anyone?). So painting a single-pixel bitmap is about the only
workaround (there's no way to draw a 1-pixel-long line or rectangle
reliably).
Well, yes and no. Assuming an untransformed, pixel-units Graphics
instance, Graphics.FillRectangle() would work fine. And assuming a
_transformed_ Graphics instance, you shouldn't be worrying about reliably
drawing exactly 1 pixel anyway (and under such a transformation, a
single-pixel bitmap is going to be transformed as well).

I don't see any real benefit to creating a whole Bitmap instance just to
draw a pixel (and that's even if you do cache it).

That said, I have a suspicion, based on the use of Graphics.FromHwnd(),
that the OP has some more fundamental problems with respect to maintaining
paint state.

Pete
Aug 5 '08 #7
On Aug 5, 8:49*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Well, yes and no. *Assuming an untransformed, pixel-units Graphics *
instance, Graphics.FillRectangle() would work fine.
It didn't when I tried it. If you specify the size of the rectangle to
draw as 1x1, then it actually paints a 2x2 pixel block. If you specify
size as 0x0, it doesn't paint it at all. Or do you propose to feed it
fractional values in hope that it will get the rounding correctly?
Aug 5 '08 #8
On Tue, 05 Aug 2008 10:52:21 -0700, Pavel Minaev <in****@gmail.comwrote:
On Aug 5, 8:49*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>Well, yes and no. *Assuming an untransformed, pixel-units Graphics *
instance, Graphics.FillRectangle() would work fine.

It didn't when I tried it. If you specify the size of the rectangle to
draw as 1x1, then it actually paints a 2x2 pixel block. If you specify
size as 0x0, it doesn't paint it at all. Or do you propose to feed it
fractional values in hope that it will get the rounding correctly?
Do you have anti-aliasing turned on? If so, then that's a variation of a
"transformed" graphics output and carries the same aspect that one should
not care about getting exactly a single pixel. If you are drawing to a
non-anti-aliased output, you should get one pixel.

Pete
Aug 5 '08 #9

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

Similar topics

4
by: grogerteal | last post by:
Hello, I am pretty new to programming and would like someone to help me get started on the program that I need to make. I hope it is not hard to do, but what I would like is a simple app that...
3
by: Mark.Larsen | last post by:
I made a class that extends DataGridColumnStyle. In the Paint override I draw a border around each cell in the column. All cells look fine except for the last one (final row). The lines I draw...
0
by: vooose | last post by:
Consider a UserControl to which you do userControl.Paint += new PaintEventHandler(paint_method) If you don't like that way, and prefer to override onPaint( ) then the problem stated below...
7
by: hamil | last post by:
The following code will display a tif file on a form. When another form is moved over the tif image, the tif image is erased where the form was moved. A paint event occurs when this happens. My...
3
by: Robert W. | last post by:
In my WinForms app I'd long ago implemented a drag & drop mechanism just like is used in PowerPoint when one wants to rearrange the order of the slides. Specifically, a thin black line is shown...
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...
7
by: Rotsey | last post by:
Hi, I have a interface that I use for a form so I can pass the form to another object. How do I add the Paint event to the interface and subsequently handle the paint event in my other...
9
by: Tom P. | last post by:
I am creating a custom control and I'm trying to get the painting of it correct. I'd like to simply use: e.Graphics.FillRectangle(Brushes.White, DisplayRectangle); .... or ......
1
by: ofiras | last post by:
In bitmap, how can I find the nearest pixel (pixel 1) to a specific pixel (pixel 2) that has different color from pixel 2? Or how can I find a pixel in a specific distance from pixel 2 (like a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.