简体   繁体   English

NullReferenceException,同时检索RGB代码并添加到数组C#

[英]NullReferenceException while retrieving RGB-code & adding to array c#

Dear StackOverflow 亲爱的StackOverflow

I am writing an algorythm that creates a "checkerboard image" (imgOutput) from another image (imgInput), like this example. 我正在编写一个算法,从另一个图像(imgInput)创建一个“棋盘图像”(imgOutput),例如示例。

What it does is checking every pixel of the image (500x500 pixels) one by one and devide the picture in 2500 boxes of 10x10 pixels each. 它所做的是一张一张地检查图像的每个像素(500x500像素),然后将图像分成2500盒,每盒10x10像素。 I already wrote the algorythm to calculate the average RGB colors, not to draw the image yet. 我已经写了算法来计算平均RGB颜色,而不是绘制图像。 This is the code: 这是代码:

    public class PixelMatrix
    {
        public int X;
        public int Y;
        public int R;
        public int G;
        public int B;
    }

    public class RGBMatrix
    {
        public int R;
        public int G;
        public int B;
    }

public Bitmap fncRasterize(Bitmap imgInput)
        {


            Bitmap imgOutput = new Bitmap(imgInput, 500, 500);
            imgOutput.Save("test.bmp");
            PixelMatrix[] arrWindows = new PixelMatrix[2500];
            RGBMatrix[] arrRGB = new RGBMatrix[100];

            Graphics gfx = Graphics.FromImage(imgOutput);


            int WindowCount = 1;
            int PixelCount = 1;

            int WindowX;
            int WindowY;

            int PixelX;
            int PixelY;

            int avrgR = 0;
            int avrgG = 0;
            int avrgB = 0;

            int tempcounter = 0;


            for (WindowY = 1; WindowY <= 50; WindowY++)
            {

                for (WindowX = 1; WindowX <= 50; WindowX++)
                {
                    PixelCount = 1;
                    avrgR = 0;

                    for (PixelY = 1;  PixelY <= 10; PixelY++)
                    {
                        for (PixelX = 1; PixelX <= 10; PixelX++)
                        {
                            MessageBox.Show("R:" + imgOutput.GetPixel(1, 1).R + " G:" + imgOutput.GetPixel(1, 1).G + " B:" +imgOutput.GetPixel(1, 1).B);

                            arrRGB[PixelCount].R = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).R;
                            arrRGB[PixelCount].G = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).G;
                            arrRGB[PixelCount].B = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).B;

                            // This is just to test
                            tempcounter = +tempcounter;
                            lblProgress.Text = tempcounter.ToString();
                        }
                    }

                    for (int tempx = 1; tempx <= 100; tempx++)
                    {
                        avrgR = +arrRGB[tempx].R;
                        avrgG = +arrRGB[tempx].G;
                        avrgB = +arrRGB[tempx].B;
                    }
                    arrWindows[WindowCount].R = (avrgR / 100);
                    arrWindows[WindowCount].G = (avrgG / 100);
                    arrWindows[WindowCount].B = (avrgB / 100);

                    WindowCount = +1;

                }
            }




            return imgOutput;

        }

Now when the algorythm starts running I get a NullReferenceException error on this line: 现在,当算法开始运行时,在此行上出现NullReferenceException错误:

 arrRGB[PixelCount].R = imgOutput.GetPixel(tempR.X, tempR.Y).R;
 arrRGB[PixelCount].G = imgOutput.GetPixel(tempG.X, tempG.Y).G;
 arrRGB[PixelCount].B = imgOutput.GetPixel(tempG.X, tempG.Y).B;

While the line above it (the Messagebox statement) flawlessly returns the RGB values. 尽管其上方的行(Messagebox语句)完美地返回了RGB值。 Could someone please explain to me why this is? 有人可以向我解释为什么吗? This is really frustrating. 这真令人沮丧。

You never created the RGBMatrix objects in arrRGB , so the R G and B properties don't exist yet. 你永远不会创造了RGBMatrix中的对象arrRGB ,所以R GB性能还不存在。 That's where the NullReferenceException is coming from. 那就是NullReferenceException的来源。

You declare an array that has 100 slots to contain elements of type RGBMatrix. 您声明一个具有100个插槽的数组,以包含RGBMatrix类型的元素。
But you have to create each of these elements and assign an instance to the correct slot before trying to access the property of the instance. 但是您必须创建这些元素中的每个元素,并将实例分配给正确的插槽,然后再尝试访问实例的属性。

for (PixelX = 1; PixelX <= 10; PixelX++)
{
    MessageBox.Show("R:" + imgOutput.GetPixel(1, 1).R + " G:" + imgOutput.GetPixel(1, 1).G + " B:" +imgOutput.GetPixel(1, 1).B);

    arrRGB[PixelCount] = new RBGMatrix();   // This create the instance

    arrRGB[PixelCount].R = (int).....
    arrRGB[PixelCount].G = (int)....
    arrRGB[PixelCount].B = (int)....

    // This is just to test
    tempcounter = +tempcounter;
    lblProgress.Text = tempcounter.ToString();
}

or the other way 或另一种方式

RGBMatrix mx = new new RBGMatrix();   
mx,R = = (int).....
....
arrRGB[PixelCount] = mx;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM