Playing with C#


I want to implement a rendering of the Mandelbrot Set using C#, leveraging the .NET framework. Since I don't know anything about C# or .NET, I'll have to figure it out in the process.


Step 1. Obtain and Install a Dev Environment

First things first, downloaded Visual C++, Express Edition. This brought .NET 2.0 framework along with it eventhough I already have 1.x.

First Impression: very very slow.

Second Impression: This isn't what I need. Turns out Visual C#, Express Edition is a different download altogether.


According to the main Express Editions page, Visual C# 2005 Express Edition is "A great combination of power and productivity for the Windows developer" while Visual C++ 2005 Express Edition  has "horsepower with a finer degree of control than other Express Editions."

I thought C# and C++ were different languages altogether. HUH!?

Step 2: Get a very basic example of graphics rendering working

  • File -> New Project
  • Empty project
  • On the Solution Explorer (right hand side), click on my project, Add -> New Item (just guessing here)
  • Class (again, just guessing)
  • Deleted the entire skeleton class and pasted in code from the link above.
  • Build -> Rebuild Solution. Got two errors that "Windows does not exist in the namespace System (are you missing an assembly refference?)"  Maybe I am...
  • In Solution Explorer, under my project. Right click on References, pick Add Refference.
  • Scroll down to System.Windows. It's not an option on its own, but System.Windows.Forms is. Picked it, since I see it mentioned in the code.
  • Build -> Rebuild Solution again. Now it's complaining about Graphics under System.Drawing.
  • Added a refference to System.Drawing.
  • Build -> Rebuild Solution. NO COMPLAINTS!
  • Debug -> Start Without Debugging


 Step 3: Figure out how that works...

 System.Drawing.Rectangle client_area = this.ClientRectangle; 
 System.Drawing.Brush background = new System.Drawing.SolidBrush(System.Drawing.Color.White); 
 g.FillRectangle(background, client_area); 

Ok, so it looks like we get the 'canvas' which represents our paintable area. We will fill it in with a "Brush," which in this case is a solid white brush. Playing around in the IDE you can quickly find another type of brush, System.Drawing.TextureBrush, which can be initialized from a bitmap image. So I threw together a little .bmp in Paint, and reduced OnPaint() to nothing more than:

 System.Drawing.Graphics g = pe.Graphics; 
 System.Drawing.Rectangle client_area = this.ClientRectangle; 
 System.Drawing.TextureBrush t = 
    new System.Drawing.TextureBrush( System.Drawing.Image.FromFile("C:\\face.bmp") ); 
 g.FillRectangle(t, client_area); 




Ok.. Let's make up some solid brushes here, one Green and one Red
 System.Drawing.Pen p1 = 
  new System.Drawing.Pen(System.Drawing.Color.Green); 
 System.Drawing.Pen p2 = new System.Drawing.Pen(System.Drawing.Color.Red); 


Draw a vertical green line, one-third through the image...

 int x1 = client_area.Width / 3; 
 System.Drawing.Point point11 = new System.Drawing.Point(x1, client_area.Y); 
 System.Drawing.Point point12 = new System.Drawing.Point(x1, client_area.Height); 
 g.DrawLine(p1, point11, point12); 


And another one, two-thirds through the image...

 x1 += x1; point11 = new System.Drawing.Point(x1, client_area.Y); 
 point12 = new System.Drawing.Point(x1, client_area.Height); 
 g.DrawLine(p1, point11, point12); 


Inscribe a red circle in the canvas (i.e. draw the biggest circle which will fit into the window)

 g.DrawEllipse(p2, client_area); 


Ok, not we create a point which is 50-pixels left and above of the center...

 System.Drawing.Point point = new System.Drawing.Point(); 
 point.X = client_area.Width/2 - 50; 
 point.Y = client_area.Height/2 - 50; 


Create a 100x100 square whose top-left corner is at the point we just made...

 System.Drawing.Size size = new System.Drawing.Size(); 
 size.Height = 100; 
 size.Width = 100; 
 System.Drawing.Rectangle rec = new System.Drawing.Rectangle(point, size); 


Draw that rectangle in red, in a 5-pixel thick brush...

 p2.Width = 5; g.DrawRectangle(p2, rec); 


Inscribe a green circle with 10-pixel border inside that rectangle...

 p1.Width = 10; g.DrawEllipse(p1, rec);


Okay, enough of that, let's go to page 2 (this page is way too big for GooglePages!)