January 30th, 2012 — James
// http://en.wikipedia.org/wiki/Mandelbrot_set
// Original Mandelbrot code by Jon Skeet
using System;
using System.Linq;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;
class MainForm : Form
{
const string AppTitle = "Mandelbrot";
BackgroundColorGenerator worker;
PictureBox pb;
MainForm()
{
this.InitializeComponent();
this.InitializeBackgroundWorker();
}
void InitializeComponent()
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Text = AppTitle;
this.pb = new PictureBox();
this.pb.Image = MandelbrotGenerator.Generate();
this.pb.Dock = DockStyle.Fill;
this.ClientSize = this.pb.Image.Size;
this.Controls.Add(pb);
this.Load += new EventHandler(this.MainForm_Load);
}
void InitializeBackgroundWorker()
{
this.worker = new BackgroundColorGenerator();
this.worker.WorkerReportsProgress = true;
this.worker.WorkerSupportsCancellation = true;
this.worker.DoWork += new DoWorkEventHandler(this.worker.GenerateColor);
this.worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ColorPalette palette = (ColorPalette)e.UserState;
if (null != palette)
{
this.pb.Image.Palette = palette;
this.pb.Invalidate(true);
}
}
void MainForm_Load(object sender, System.EventArgs e)
{
this.worker.RunWorkerAsync(this.pb.Image.Palette);
}
public static void Main()
{
MainForm form = new MainForm();
Application.Run(form);
}
class BackgroundColorGenerator : BackgroundWorker
{
const int Timeout = 1000;
public void GenerateColor(object sender, DoWorkEventArgs args)
{
ColorPalette palette = (ColorPalette)args.Argument;
Random rand = new Random();
while (!this.CancellationPending)
{
palette.Entries[0] = Color.FromArgb((rand.Next(255) * rand.Next(7)) % 256,
(rand.Next(255) * rand.Next(7)) % 256,
(rand.Next(255) * rand.Next(7)) % 256);
for (int i = 1; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb((rand.Next(i) * rand.Next(7)) % 256,
(rand.Next(i) * rand.Next(7)) % 256,
(rand.Next(i) * rand.Next(7)) % 256);
}
this.ReportProgress(0, palette);
Thread.Sleep(BackgroundColorGenerator.Timeout);
}
}
}
static class MandelbrotGenerator
{
const int MaxIterations = 1000;
const double SampleWidth = 3.2;
const double SampleHeight = 2.5;
const double OffsetX = -2.1;
const double OffsetY = -1.25;
const int ImageWidth = 900;
const int ImageHeight = (int)(SampleHeight * ImageWidth / SampleWidth);
static byte ComputeMandelbrotIndex(int row, int col)
{
double x = (col * SampleWidth) / ImageWidth + OffsetX;
double y = (row * SampleHeight) / ImageHeight + OffsetY;
double y0 = y;
double x0 = x;
for (int i = 0; i < MaxIterations; i++)
{
if (x * x + y * y >= 4)
{
return (byte)((i % 255) + 1);
}
double xtemp = x * x - y * y + x0;
y = 2 * x * y + y0;
x = xtemp;
}
return 0;
}
public static Bitmap Generate()
{
var points = from row in Enumerable.Range(0, ImageHeight)
from col in Enumerable.Range(0, ImageWidth)
select new { row, col };
var query = from point in points.AsParallel().AsOrdered()
select ComputeMandelbrotIndex(point.row, point.col);
byte[] data = query.ToArray();
unsafe
{
fixed (byte* ptr = data)
{
IntPtr scan0 = new IntPtr(ptr);
return new Bitmap(ImageWidth, ImageHeight, ImageWidth,
PixelFormat.Format8bppIndexed, scan0);
}
}
}
}
}
Download the complete project
April 11th, 2011 — James
If you are using Visual Studio 2008, just open the project file and an entry to the assembly you need to refer to.
Locate the Item Group that has references. Take any one of the system related assembly reference and change the Include entry.
<Reference Include="Your.GAC.Assembly.Name" />
March 17th, 2011 — James
public string GetFileName(string path)
{
FileInfo finfo = new FileInfo(path);
string FileName = finfo.Name;
string FileExtn = finfo.Extension;
return FileName.Replace(FileExtn, string.Empty);
// Or just return finfo.Name.Replace(finfo.Extension, string.Empty);
}
Sample:
Input: c:\temp\sampletext.txt
Output: sampletext
namespace needed: System.IO
January 26th, 2011 — James
Here is the code sample to get the name of the directory from a full path. The actual directory do not have to exist to execute this code:
string fullPath = @"c:\rootFolder\secondLevel\Thirdlevel";
Console.WriteLine(new DirectoryInfo(fullPath).Name);
This should output just “Thirdlevel”.
But if the path has file name in it, then you need to strip that away.
August 6th, 2010 — James
I needed the full name of the local computer which is in a domain. I could not find a straight forward way to get it. I found the following to work. Haven’t failed so far on Windows client and server OS (XP+ and Windows 2003+.)
Dns.GetHostEntry(Environment.MachineName).HostName;
Dns.GetHostEntry("127.0.0.1").HostName;