[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

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);

0
JonMny
Asked:
JonMny
  • 2
1 Solution
 
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
 
monarch_ilhanCommented:
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now