469,582 Members | 2,477 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

Using SetPixel

Hello everyone,

I have to do an image viewer for an exotic format so I want to know
how to place pixels somewhere on the form or on some object.

I tried doing this but it works incredibly slow.

{

Bitmap bmp = new Bitmap(this.Width, this.Height);

for (int i = 0; i < this.Width; i++)

{

for (int j = 0; j < this.Height; j++)

{

bmp.SetPixel(i, j, Color.Blue);

}

}

Graphics g = this.CreateGraphics();

g.DrawImage(bmp, new Point(0, 0));

bmp.Dispose();

}

May 7 '07 #1
3 3919
Seabass,

The problem with this is that as soon as your form receives a refresh
windows message, it will paint over whatever you drew already.

What you need to do is attach to the Paint event, or override the
OnPaint method for the control/form you want to display this in.

You might not want to create the bitmap every time, but rather, only
when something changes.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Seabass" <sh****@upei.cawrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
Hello everyone,

I have to do an image viewer for an exotic format so I want to know
how to place pixels somewhere on the form or on some object.

I tried doing this but it works incredibly slow.

{

Bitmap bmp = new Bitmap(this.Width, this.Height);

for (int i = 0; i < this.Width; i++)

{

for (int j = 0; j < this.Height; j++)

{

bmp.SetPixel(i, j, Color.Blue);

}

}

Graphics g = this.CreateGraphics();

g.DrawImage(bmp, new Point(0, 0));

bmp.Dispose();

}

May 7 '07 #2
On Mon, 07 May 2007 10:48:39 -0700, Seabass <sh****@upei.cawrote:
I have to do an image viewer for an exotic format so I want to know
how to place pixels somewhere on the form or on some object.

I tried doing this but it works incredibly slow. [...]
In addition to what Nicholas wrote (he doesn't seem to have addressed your
specific question, but his comments *are* relevant to what you're doing,I
think)...

The reason your code is so slow is that you're setting one pixel at a
time. It's very expensive, performance-wise, to go between the
application and the bitmap, and you're incurring that cost once for every
pixel in the image. Icing on the cake is doing all that work to a bitmap
and then copying the bitmap to your destination, but that's an
insignificant cost compared to the pixel-at-a-time approach.

Fortunately, all graphics APIs offer better ways to do this sort of thing,
including .NET's. :)

In your case, you seem to be setting all of the pixels in the image to
blue. So rather than the nested loops calling SetPixel(), use this:

using (SolidBrush brush = new SolidBrush(Color.Blue))
{
g.FillRectangle(brush, new Rectangle(new Point(0,0), this.Size));
}

Noting of course the advice Nicholas gave you. If you want your effort to
be persistent, you need to at a minimum to the above in the OnPaint()
method for your form. If you're doing something more complicated than
just filling it with blue, you may well want to cache your drawing to a
bitmap (you can draw to a bitmap by calling the Graphics.FromImage()
method to get a Graphics instance that will draw to the bitmap), and then
just use the DrawImage() method to draw that image to the form in its
OnPaint() method.

Pete
May 7 '07 #3
On 7 Mai, 20:37, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Mon, 07 May 2007 10:48:39 -0700, Seabass <sha...@upei.cawrote:
I have to do an image viewer for an exotic format so I want to know
how to place pixels somewhere on the form or on some object.
I tried doing this but it works incredibly slow. [...]

In addition to what Nicholas wrote (he doesn't seem to have addressed your
specific question, but his comments *are* relevant to what you're doing, I
think)...

The reason your code is so slow is that you're setting one pixel at a
time. It's very expensive, performance-wise, to go between the
application and the bitmap, and you're incurring that cost once for every
pixel in the image. Icing on the cake is doing all that work to a bitmap
and then copying the bitmap to your destination, but that's an
insignificant cost compared to the pixel-at-a-time approach.

Fortunately, all graphics APIs offer better ways to do this sort of thing,
including .NET's. :)

In your case, you seem to be setting all of the pixels in the image to
blue. So rather than the nested loops calling SetPixel(), use this:

using (SolidBrush brush = new SolidBrush(Color.Blue))
{
g.FillRectangle(brush, new Rectangle(new Point(0,0), this.Size));
}

Noting of course the advice Nicholas gave you. If you want your effort to
be persistent, you need to at a minimum to the above in the OnPaint()
method for your form. If you're doing something more complicated than
just filling it with blue, you may well want to cache your drawing to a
bitmap (you can draw to a bitmap by calling the Graphics.FromImage()
method to get a Graphics instance that will draw to the bitmap), and then
just use the DrawImage() method to draw that image to the form in its
OnPaint() method.

Pete
In case you have to work with small portions of the image (maybe even
pixel by pixel), you should consider looking at this link:
http://www.bobpowell.net/lockingbits.htm
While it is a little complicated, working with LockBits is blazing
fast compared to the normal SetPixel approach.
I use this myself to implement custom grayscale conversions and such.

Sincerely,
Kevin Wienhold

May 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter | last post: by
2 posts views Thread by pixel | last post: by
reply views Thread by AnthonyBenbrook | last post: by
reply views Thread by mark | last post: by
2 posts views Thread by Matthew | last post: by
5 posts views Thread by pcnerd | last post: by
10 posts views Thread by pcnerd | last post: by
3 posts views Thread by Atmapuri | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.