• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# C# Bresenham algorithm

## 4 posts in this topic

Hey all, this is gonna be my first post ... I''ve been lurking here for a bit now and have started experimenting with some basic game/graphics programming. I''ll start off by saying that I specifically want to use GDI+ as opposed to MDX because I want to learn the concepts behind it all first before I move on to a powerhouse platform like MDX or C++ or whatever. Now ... I''ve implemented (ok, copied ;-) ) the C function found here [http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html] in C# ... only problem is, that it''s rather slow. I''ve got a demo that draws a line between a point, and the location of the mouse. Once it gets longer than about 100 pixels, it starts to visibly lag behind the mouse as it redraws each frame.
void Line(Graphics g, int x1, int y1, int x2, int y2, Pen pen)
{
int dx = x2 - x1;
int dy = y2 - y1;

int y = y1;
int eps = 0;
Bitmap bmp = new Bitmap(1,1,g);
bmp.SetPixel(0,0,Color.Black);

for (int x=x1; x<=x2;x++)
{
g.DrawImage(bmp,x,y);

eps+=dy;
if ((eps*2) &rts= dx)
{
y++;
eps-=dx;
}
}

}

The only thing I can guess is causing it to be slow is the use of a Bitmap object to draw the pixel as there''s no way to set an individual pixel using only the Graphics object. I was wondering if anyone else here had enough C#/GDI+ experience to see if there was a way to speed this up. Thanks in advance Joel Martinez http://www.codecube.net/
0

##### Share on other sites
That is because you are sort of abusing GDI+ It''s not made for lots of direct pixel access. If you want that, DirectX will be better. If you just want to draw a line, GDI+ has functions for that already...and they are fast.
0

##### Share on other sites
Right ... I totally understand that ... but I didn''t want to use the built in line function (you''re right, it is very fast) because I want to understand the underlying concept behind it all before I move on to something like DirectX.

And as far as using GDI+ ... I would assume that a 2ghz machine with 512 megs of ram using GDI+ would outperform a nintendo, and I don''t think drawing a line is beyond nintendo performance. I could be wrong though ;-)

Joel Martinez
http://www.codecube.net/
0

##### Share on other sites
Every time you call bmp.SetPixel, you aren''t just doing direct pixel access.

You are first locking the entire surface in memory. SetPixel doesn''t just lock the 1 pixel rectangle you are working on.

Then you are modifying the pixel.

Finally, you are unlocking the surface.

The lock/unlock is what is taking all of the time.

If you want to do this fast, either do it in a backbuffer then blit it to the screen, or lock the buffer once, then use direct memory access to change the pixels, then unlock the buffer.

RomSteady - Test Locally, Test Globally, Test Early, Test Often
0

##### Share on other sites
Yeah the constant bitmap locking is killing you, as RomSteady has said.

I''d suggest you create a Bitmap equal to the size of the Graphics''s surface area and then use the Bitmap::LockBits() method to get a handle on the raw bitmap data buffer (see the GDI+ documentation for Bitmap::LockBits and BitmapData class for exact usage information), plot the pixels into that buffer, unlock and THEN DrawImage from the Bitmap to the Graphics object. If done correctly this should be _significantly_ faster than what you are doing now. Doing it this way requires an understanding of pixel formats and surface pitch vs surface width, so if you''re not already familiar with these concepts you may have some reading up to do, but the GDI+ documentation and the examples it includes should be enough to get you going.

0