Mandelbrot Zoomer in WPF
Mandelbrot Zoomer in WPF
Written by Mike James   
Thursday, 23 July 2015
Article Index
Mandelbrot Zoomer in WPF
Where Next

Where next?

With the program as described above you can go exploring the Mandelbrot set to your heart’s content.

There’s a lot to see, but be warned areas that are mostly blue take a long time to plot because these take the iteration loop a full 1000 times around!

The fact that the computation takes so long makes it an ideal candidate for running on a thread other than the UI thread. You may notice that the UI freezes when the set is being computed and the only way to keep the application responsive is to use a worker thread.

When you are just trying to get it all working it is worth making the Image control small so that plots occur quickly.

You can add some additional features to make the program more usable – a plot interrupt button, for example, a pan, and so on..



There are lots of interesting areas to explore.


There is one small feature that I should warn you about before it wastes a lot of time when you try to “debug” the program. There are no limits on how far you can zoom in on any given region of the Mandelbrot set. You will discover that you can indeed use very high zooms, but sooner or later you will select a very small region and the result will not look fractal but “blocky”. No you haven’t discovered that there is a limit to the structure of the Mandelbrot set!

What is happening is that you have zoomed to the point where double precision variables aren’t accurate enough to represent the position of each point. The result is that you get the same answer for whole rectangular regions for the iteration and hence the blocky structure in the final plot. The solution to the problem is fairly simple – increase the precision - but this is easier said than done and you just hit another limit sooner or later. .



No not the end of fractals – you’ve just zoomed too far



using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Numerics;

namespace MandelZoomer{
 public partial class MainWindow : Window    {       
  private Rect area = new Rect(
                 new Point(-2.4, -1.5),
                 new Point(0.8, 1.5));

  private Rectangle selection =
            new Rectangle()
             Stroke = Brushes.Black,
             StrokeThickness = 1,
             Visibility = Visibility.Collapsed

  private bool mousedown = false;
  private Point mousedownpos;

  public MainWindow()
   image1.Source = drawSet(area);

  Int32 mandelbrot(Complex c)
   Int32 count = 0;
   Complex z = Complex.Zero;
   while (count < 1000 && z.Magnitude < 4)
    z = z * z + c;
   return count;

  WriteableBitmap drawSet(Rect area)
   int PixelHeight = (int)image1.Height;
   int PixelWidth = (int)image1.Width;
   WriteableBitmap wbmap = new WriteableBitmap(
           96, 96,

   int BytesPerPixel = wbmap.Format.BitsPerPixel / 8;
   byte[] pixels = new byte[PixelHeight *
                            PixelWidth *
   int s = PixelWidth * BytesPerPixel;
   double xscale = (area.Right - area.Left)
                               / PixelWidth;
   double yscale = (area.Top - area.Bottom)
                              / PixelHeight;
   for (int i = 0; i < pixels.Length;
                        i += BytesPerPixel)
    int py = i / s;
    int px = i % s / BytesPerPixel;
    double x = area.Left + px * xscale;
    double y = area.Top - py * yscale;
    Complex c = new Complex(x, y);
    int count = mandelbrot(c);
    Color C = colorMap(count);
    pixels[i] = C.B;
    pixels[i + 1] = C.G;
    pixels[i + 2] = C.R;
    pixels[i + 3] = C.A;
   wbmap.WritePixels(new Int32Rect(0, 0,
      PixelWidth, PixelHeight), pixels, s, 0);
   return wbmap;

  Color colorMap(int count)
   Color C = new Color();
   C.B = (byte)(count / 100 * 25);
   count = count % 100;
   C.G = (byte)(count / 10 * 25);
   C.R = (byte)(count % 10 * 25);
   C.A = 255;
   return C;

  private void button1_Click(object sender,
                            RoutedEventArgs e)
   area = new Rect(new Point(-2.4, -1.5),
                       new Point(0.8, 1.5));
   image1.Source = drawSet(area);

  private void Canvas1_MouseLeftButtonDown(
        object sender, MouseButtonEventArgs e)
   mousedown = true;
   mousedownpos = e.GetPosition(Canvas1);
   Canvas.SetLeft(selection, mousedownpos.X);
   Canvas.SetTop(selection, mousedownpos.Y);
   selection.Width = 0;
   selection.Height = 0;
   selection.Visibility = Visibility.Visible;

  private void Canvas1_MouseMove(
           object sender, MouseEventArgs e)
   if (mousedown)
    Point mousepos = e.GetPosition(Canvas1);
    Vector diff = mousepos - mousedownpos;
    Point TopLeft = mousedownpos;
    if (diff.X < 0)
     TopLeft.X = mousepos.X;
     diff.X = -diff.X;
    if (diff.Y < 0)
     TopLeft.Y = mousepos.Y;
     diff.Y = -diff.Y;
    selection.Width = diff.X;
    selection.Height = diff.Y;
    Canvas.SetLeft(selection, TopLeft.X);
    Canvas.SetTop(selection, TopLeft.Y);

  private void Canvas1_MouseLeftButtonUp(
         object sender, MouseButtonEventArgs e)
   mousedown = false;
   selection.Visibility = Visibility.Collapsed;
   double xscale = (area.Right - area.Left) /
   double yscale = (area.Top - area.Bottom) /
   Point TopLeft = new Point(area.Left +
    Canvas.GetLeft(selection) * xscale,
    area.Top - Canvas.GetTop(selection)
                                * yscale);
   Point BottomRight = TopLeft + new Vector(
     selection.Width * xscale,
            -selection.Height * yscale);
   area = new Rect(TopLeft, BottomRight);
   image1.Source = drawSet(area);



To access the code for this project, once you have registered,  click on CodeBin.




WPF The Easy 3D Way

WPF provides a full 3D graphics experience without the need to master DirectX. It really is the easy 3D approach.

Inside Dependency Properties

Learn how dependency properties really work by creating a custom dependency property


WriteableBitmap gives you a bitmap object that you can modify dynamically - but exactly how to do this isn't always obvious.

Loading Bitmaps: DoEvents and the closure pattern

Sometimes loading a bitmap causes an asynchronous download and you have to wait for it to complete before using it - but how best to wait? The standard solution is to use an event but this breaks what [ ... ]

BitmapSource: WPF Bitmaps

Although bitmap graphics are not a main focus of WPF it does offer some useful facilities

Other Articles


blog comments powered by Disqus








l the code

Last Updated ( Thursday, 23 July 2015 )

RSS feed of all content
I Programmer - full contents
Copyright © 2016 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.