Performance problem while working with Bitmap and Graphics Object

Last post 05-09-2007 1:48 AM by shrainpatel. 4 replies.

Sort Posts:

  • Performance problem while working with Bitmap and Graphics Object

    03-27-2007, 2:38 AM
    • Loading...
    • atsofttech
    • Joined on 07-16-2005, 7:48 PM
    • India
    • Posts 267

    Hi,

    I am writing code to Gray Scale an Image. For that I am doing loop on each pixel as shown in code below. It is working but time required to do this is very long compare to other Graphics Processing Programs.
    Is there other fast way to do this?

    ...

    ...

    for

    (int y = 0; y < m_Bitmap.Height; y++)

    {

    for (int x = 0; x < m_Bitmap.Width; x++)

    {

    Color c = m_Bitmap.GetPixel(x, y);

    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);

    m_Bitmap.SetPixel(x, y,

    Color.FromArgb(luma, luma, luma));

    }

    }

     
    Everything is possible!
    Filed under: , , , ,
  • Re: Performance problem while working with Bitmap and Graphics Object

    03-27-2007, 4:15 AM
    • Loading...
    • Svante
    • Joined on 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 1,843
    • Moderator

    I'm no grahics wiz, I'm sure there's a cool way to do this with GDI+, but at least you can start by getting rid of the floats, and move out the temp-allocations. I.e.:

    Color c;
    int luma;
    for (int x = 0; x < m_Bitmap.Width; x++) 
    {
        c = m_Bitmap.GetPixel(x, y); 
        luma = (c.R * 30 + c.G * 59 + c.B * 11)/100;
        m_Bitmap.SetPixel(x, y, Color.FromArgb(luma, luma, luma)); 
    }
    
    

     You might also gain a bit by pre-generating the Color-structures (since you're doing Color.FromArg(luma, luma, luma) there are only 256 possible values). Create an array with 256 entires, and place ready-mady Color-structures there. Something like this:

    // Do this first - outside both loops
    Color[] colors = new Color[256];
    for (int i = 0; i < colors.Length; i++)
    {
        colors[i] = Color.FromArgb(i, i, i);
    }
    
    // then you can do...
    m_Bitmap.SetPixel(x, y, colors[luma]);
     
    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: Performance problem while working with Bitmap and Graphics Object

    03-27-2007, 4:47 AM
    • Loading...
    • atsofttech
    • Joined on 07-16-2005, 7:48 PM
    • India
    • Posts 267

    Thanks. This makes sense and helpful.

    But I am still looking for fast way. Please let me know if anybody have idea.

    Everything is possible!
  • Re: Performance problem while working with Bitmap and Graphics Object

    03-28-2007, 12:57 AM
    Answer
    • Loading...
    • atsofttech
    • Joined on 07-16-2005, 7:48 PM
    • India
    • Posts 267
    Everything is possible!
  • Re: Performance problem while working with Bitmap and Graphics Object

    05-09-2007, 1:48 AM
    • Loading...
    • shrainpatel
    • Joined on 05-08-2007, 8:57 AM
    • India
    • Posts 119

    Hi,

     Its really great article, It helps me lot.

    thanks buddy.

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter