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

Home Posts Topics Members FAQ

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.FromHw nd(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 3549
On Aug 5, 12:11*pm, ofiras <ofi...@gmail.c omwrote:
Hi,
I made a "Paint" program, but I couldn't find a method to paint 1
pixel using graphic state ("Graphics g =
Graphics.FromHw nd(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.c omwrote:
On Aug 5, 12:11*pm, ofiras <ofi...@gmail.c omwrote:
Hi,
I made a "Paint" program, but I couldn't find a method to paint 1
pixel using graphic state ("Graphics g =
Graphics.FromHw nd(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.FromIm age 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.c omwrote in message
news:e5******** *************** ***********@c58 g2000hsc.google groups.com...
Hi,
I made a "Paint" program, but I couldn't find a method to paint 1
pixel using graphic state ("Graphics g =
Graphics.FromHw nd(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.c omwrote:
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.FromIm age 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.c omwrote:
[...]
>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.FillRe ctangle() 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.FromHw nd(),
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...@nn owslpianmk.com>
wrote:
Well, yes and no. *Assuming an untransformed, pixel-units Graphics *
instance, Graphics.FillRe ctangle() 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.c omwrote:
On Aug 5, 8:49*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
>Well, yes and no. *Assuming an untransformed, pixel-units Graphics *
instance, Graphics.FillRe ctangle() 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
"transforme d" 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
8226
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 has a label which returns the color of a pixel that is in position x,y on the screen (i.e. the desktop @ 1024 x 768 pixels), where x and y are of course variable.
3
2923
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 in the far right and bottom of the cell don't show. I can draw 1 pixel up from the bottom and 1 pixel left of the right border. What is preventing me from painting the border around this last cell?
0
1928
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 still exists (I tried both ways). Imagine you have a Console.WriteLine( ) in the paint method so you know when its getting called. Also there is no place in the code that userControl.Paint is -+ so the event is *never* removed.
7
1830
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 question is..how would I save the tif images as soon as it is initially painted, and then restore the image in a paint event. It seems to me that there should be some sort of a SaveImage method and a RestoreImage method that would be fast. Note:...
3
6864
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 between thumbnails to indicate where an item will be dropped. As the user moved away I'd erase it by simply drawing another line using the BackColor of the panel. This worked great! But I've recently switched to using a Gradient panel and so now...
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
7
2356
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 object.? I am not sure of the syntax required to do this?
9
4269
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 ... e.Graphics.FillRectangle(Brushes.White, ClientRectangle); .... or ... e.Graphics.FillRectangle(Brushes.White, Bounds); But each of those fails to draw a rectangle all the way around (most are 1 pixel off). Instead I find myself having to use:
1
3246
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 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...
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.