You are quite right in your concern about efficiency. This way is no-go, but it's easy to fix.
I did not pay attention for the algorithm itself, only to one big problem: the use of
GetPixel
. This method is prohibitively slow. Instead, you should use lock the bitmap data into an array and work with the whole bitmap data in memory. Use
System.Drawing.Bitmap.LockBits
:
http://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx[
^].
In these two MDSN articles, you will find a simple code sample.
—SA