Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Community,
I am encountering a problem while doing some calculation with a bitmap. I want to get the summed up brightness of a bitmap (659 x 494) and a matrix with the brightness values. To do that I combined some helpful articles from here.

Here is the code:
C#
public static double CALC_BRIGHTNESS(Bitmap bm)
        {
            double lum = 0;
            var tmpBmp = new Bitmap(bm);
            var width = bm.Width;
            var height = bm.Height;
            var bppModifier = bm.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var srcData = tmpBmp.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat);
            var stride = srcData.Stride;
            var scan0 = srcData.Scan0;

            unsafe
            {
                int bytes = Math.Abs(stride) * tmpBmp.Height;
                byte[] p = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(scan0, p, 0, bytes);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int idx = (y * stride) + x * bppModifier;
                        double lumin = (0.299 * p[idx + 2] + 0.587 * p[idx + 1] + 0.114 * p[idx]) / 255.00;
                        pixels[x, y] = lumin;
                        if (lumin >= border)
                            lum += lumin;
                    }
                    
                }
            }

            tmpBmp.UnlockBits(srcData);
            tmpBmp.Dispose();
            return lum;
        }


But it is always exiting the for loops where y = 491, x = 494 and idx = 327640. Following calculations are not continued, the Form is shown and can be closed...
With a smaller picture(approx 300 x 200) I don't have any issues.
Maybe one of you can help me solve this issue.
Posted

1 solution

Start to put a try catch around your code and show any error in a MessageBox.
C#
public static double CALC_BRIGHTNESS(Bitmap bm)
{
    Bitmap tmpBmp = null;  // Declare this variable outside the try-catch block
                           // and call dispose in finally
    try
    {
        // Your code
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString));
    }
    finally
    {
        if (tmpBmp != null)
            tmpBmp.Dispose();
    }
}


Also how and where is the variable pixels declared?
 
Share this answer
 
v3
Comments
DANIEL_PD 12-Oct-15 4:35am    
Hello George, thanks for your answer.
I had a try...catch around the code that calls this function, but obviously it wasn't enough...

pixels is initialized like following:

public static double[,] pixels;

and in the main code:

[...]
p_bmp = (Bitmap)Image.FromFile("C:\\Glasplatte\\TEST.tiff");
pixels = new double[p_bmp.Width, p_bmp.Height];
double val = CALC_BRIGHTNESS(p_bmp);
[...]


The following error is displayed:
System.IndexOutOfRangeException: Index was outside the bounds of the array...

I found out, that int bytes = Math.Abs(stride) * tmpBmp.Height; equals 327640 and then p[idx +2] is out of range... but why is the array to small?
George Jonsson 12-Oct-15 6:03am    
Well, what is the possible max-value of int idx = (y * stride) + x * bppModifier;
It has to be less than bytes.Length + 2.
DANIEL_PD 13-Oct-15 2:19am    
Thanks for the Answer, I set the maximum to a fix value and it worked... Thank you
George Jonsson 13-Oct-15 5:47am    
Glad it helped you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900