Search code examples
c#bitmapdata

Bitmap not parsed as a function variable


I got some simplistic code to get an average color out of 5 bitmaps.

    private Bitmap AVG5Bitmaps(Bitmap a, Bitmap b, Bitmap c, Bitmap d, Bitmap e)
    {
        Bitmap result = new Bitmap(c);

        for (int x = 0; x < result.Width; x++) 
        {
            for (int y = 0; y < result.Height; y++) 
            {
                int r = (
                        (int)a.GetPixel(x, y).R + 
                        (int)b.GetPixel(x, y).R +
                        (int)c.GetPixel(x, y).R +
                        (int)d.GetPixel(x, y).R +
                        (int)e.GetPixel(x, y).R) / 5;

                int g = (
                         (int)a.GetPixel(x, y).G +
                         (int)b.GetPixel(x, y).G +
                         (int)c.GetPixel(x, y).G +
                         (int)d.GetPixel(x, y).G +
                         (int)e.GetPixel(x, y).G) / 5;

                int b =  (
                         (int)a.GetPixel(x, y).B +
                         (int)b.GetPixel(x, y).B +
                         (int)c.GetPixel(x, y).B +
                         (int)d.GetPixel(x, y).B +
                         (int)e.GetPixel(x, y).B) / 5;

                  result.SetPixel(x,y,Color.FromArgb(r,g,b))
            }
        }
       return result; 
    }

The strange thing is that a.GetPixel(x, y).R is recognized, however with b.GetPixel(x, y).R gives an error:

Cannot use local variable b before it is declared

b is not even seen as a bitmap object? I don't get it, why does the code work with a but not with b. Is this a bug with Visual Studio 2010?

on request updated the question with complete function code


Solution

  • Your line:

    int b = 
    

    is hiding the parameter b, thus you are trying to access that variable within its assignmnent. Thus the error. Rename the variable.