[C#] Bitmap imaging

Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
I've been playing around with fractal patterns in C# and I've built a program to render the Mandelbrot set, which involves rendering the pattern on a pixel-by-pixel basis (the algorithm i've written does this randomly to produce a nice effect :p).

Currently I'm using a Bitmap object and using the method SetPixel() to set the colour of each pixel individually, however this seems to be a very slow way of doing it... is there any better way? :confused:

Cheers :)
 
Last edited:
For something like the mandelbrot where you're doing a bunch of calculations for each pixel, I don't quite know how else other than SetPixel() you can set the colour of each pixel??
 
Well, setting each pixel individually is obviously the only war of doing it, but I was just wondering if there was a more efficient way of doing that than the Bitmap/SetPixel() method.
 
I knocked this up a few months ago and ended up using DirectDraw. Bear in mind that .net isn't really designed for speed so even using that won't make it particularly fast, but it does help. It also allows you to do things like mouse control and re-rendering optimisations rather more easily :)

arty
 
Inquisitor said:
I was just wondering if there was a more efficient way of doing that than the Bitmap/SetPixel() method.
You can use the LockBits() method of the Bitmap object and then access pixel data directly.

As already mentioned, one alternative might be to use the managed DirectX classes.

cheers
v.f.
 
Thanks for the help! Haven't had time to try any of your suggestions out yet... I'll have a look at the LockBits() method I think... seems to require unsafe code though...

In the mean time, have a Mandelbrot image :D
http://img115.imageshack.us/img115/9400/mandelbrot7zw.png

Still only does monochrome, and looks a bit messy as it's not anti-aliased. Does anyone have any idea how I could anti-alias it? :confused:
 
Inquisitor said:
Still only does monochrome, and looks a bit messy as it's not anti-aliased. Does anyone have any idea how I could anti-alias it? :confused:

The 'easy' way would be to render at 4x the resolution and resample downwards. That gives very good results but is obviously horrifically expensive in terms of performance :p

arty
 
It actually looks a bit like the iterations aren't high enough, but they're set to 8192 which is plenty. Maybe try increasing it because of the level you're zoomed in?
 
I've tried setting them to 65536, and here is the result:
http://img99.imageshack.us/img99/5715/mandelbrotimage6cx.png

Seems to have helped a bit, but it still looks messy. Also, the higher the iteration count, the darker the image seems to become. I can overcome this by increasing the radiance (which amplifies the brightness of each pixel finding (iterationCount/iterationLimit)^(1 - radiance) * 255). Hower, this seems to reduce the contrast somewhat, especially at very high numbers of iterations. :confused:
 
Looks nice, is it really worth worrying about the graphics methods you are using though?

I would have thought that for something like fractal generation time time to calculate what colour the pixel should be far outweighs the time to actually plot the pixel so changing to a more efficient method won't actually save you all that much time?
 
You'd think so, but unless you're doing a stupidly high number of iterations, the time to plot each pixel does actually have a noticeable impact on the overall generation time.

For example, plotting a 300x300 square (of a solid colour) in C# using the method I mentioned, pixel by pixel, takes around 3-5 seconds.

robmiller said:
Does PHP feel limiting now? :D
Well it is only a web scripting language :p
 
Inquisitor said:
For example, plotting a 300x300 square (of a solid colour) in C# using the method I mentioned, pixel by pixel, takes around 3-5 seconds.

Ouch, that does seem a long time - no wonder you're looking for a better method then :p
 
Also, the prettiness of your program has inspired me and I'm going to have a play with fractal images. I've been looking for a pet project in C# for a while :)
 
It's certainly very rewarding to get visual results from your hard work :)
That LockBits method also meant I got to play around with unsafe code /evilgrin :D
 
Massive improvement - at least 10 times faster :)
Out of interest, would it be best to lock a 1x1 Rectangle into memory for each pixel, or should I just lock the entire image into memory and keep using the same BitmapData object every time I set a pixel?
 
Last edited:
Back
Top Bottom