Speed up bitmap manuipulation

I have a routine that changes the color of a section of a bitmap. I code works perfectly, however it is very slow.  Can anyone help me speed this code up?

{
                //Create temp file to work with
                string tempFile =Path.Combine(Directory.GetCurrentDirectory() ,"tmp.bmp");
                try
                {
                    File.Copy(filename, tempFile, true);
                }
            catch(Exception er)
                {
                    MessageBox.Show("Failed to create preview.");
                    ApplicationLogger.LogEvent(er.Message);  
                    return;
                }
                Bitmap mg1 = new Bitmap(tempFile);
                int x = 0;
                int y = 0;
                for (x = 0; x <= mg1.Width - 1; x++)
                {
                    for (y = 0; y <= mg1.Height - 1; y++)
                    {
                        Color cl = mg1.GetPixel(x, y);
                        if (cl == Color.FromArgb(255, 254, 254, 254) | cl == Color.FromArgb(255, 255, 255, 255))
                        {
                            mg1.SetPixel(x, y, Color.DarkGray);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                for (y = mg1.Height - 1; y >= 0; y += -1)
                {
                    for (x = 0; x <= mg1.Width - 1; x += 1)
                    {
                        Color cl = mg1.GetPixel(x, y);
                        if (cl == Color.FromArgb(255, 254, 254, 254) | cl == Color.FromArgb(255, 255, 255, 255) | cl == Color.FromArgb(255, 255, 0, 0))
                        {
                            mg1.SetPixel(x, y, Color.DarkGray);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                for (y = mg1.Height - 1; y >= 0; y += -1)
                {
                    for (x = mg1.Width - 1; x >= 0; x += -1)
                    {
                        Color cl = mg1.GetPixel(x, y);
                        if (cl == Color.FromArgb(255, 254, 254, 254) | cl == Color.FromArgb(255, 255, 255, 255) | cl == Color.FromArgb(255, 255, 0, 0))
                        {
                            mg1.SetPixel(x, y, Color.DarkGray);
                        }
                        else
                        {
                            break;  
                        }
                    }
                }

                try
                {
                    File.Delete(Path.Combine(FileIO.GetDocumentsFolder(), "preview.bmp"));
                    mg1.Save(filename);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to create preview. See application log file for details.");
                    ApplicationLogger.LogEvent(ex.Message);

LVL 9
JonMnyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
C#, as all .net languages,  is inherently slow for high-performance processing like reading a bitmap pixel by pixel.
The same application in C++ should be slow also, but with C++ you have the possibility to access the bitmap buffer directly, without invoking GetPixel, this will be far faster than first approach.
There is a not well documented feature in C#, called "unsafe mode". In this mode, you can process memory chunks directly, outside the .net context, so you can achieve similar performance than C++.

0
monarch_ilhanCommented:
As jaime_olivares mentioned using .NET GDI classes bring a performance issue while it handles image processing easier and as managed.
If you use unsafe code (direct memory access) you will eliminate this performance problem.
here is a sample code which can help you in solving your problem.

http://blog.paranoidferret.com/index.php/2007/08/31/csharp-tutorial-convert-a-color-image-to-greyscale/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monarch_ilhanCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.