【问题标题】:NullReferenceException while retrieving RGB-code & adding to array c#检索 RGB 代码并添加到数组 c# 时出现 NullReferenceException
【发布时间】:2023-03-06 20:42:01
【问题描述】:

亲爱的 StackOverflow

我正在编写一个从另一个图像 (imgInput) 创建“棋盘图像” (imgOutput) 的算法,例如 this 示例。

它的作用是逐个检查图像的每个像素(500x500 像素),并将图片分成 2500 个 10x10 像素的盒子。我已经编写了算法来计算平均 RGB 颜色,而不是绘制图像。这是代码:

    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;

        }

现在当算法开始运行时,我在这一行收到 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;

虽然它上面的行(Messagebox 语句)完美地返回了 RGB 值。有人可以向我解释这是为什么吗?这真是令人沮丧。

【问题讨论】:

  • 'imgOutput' 字段是否已初始化?
  • 所以贴上调试并检查线路。无需您或我们猜测。
  • 我可以保存它,你可以在 imgOutput.Save("test.bmp"); 行看到

标签: c# arrays class bitmap rgb


【解决方案1】:

您从未在arrRGB 中创建RGBMatrix 对象,因此R GB 属性还不存在。这就是NullReferenceException 的来源。

【讨论】:

    【解决方案2】:

    您声明了一个包含 RGBMatrix 类型元素的具有 100 个插槽的数组。
    但是在尝试访问实例的属性之前,您必须创建每个元素并将实例分配给正确的插槽。

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

    或者其他方式

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

    【讨论】:

    • 如果我在 for 循环中声明这些元素,我还能在循环外访问它吗? (但在同一个子)
    • 您声明并初始化一个元素(意味着您有一个为 RGBMatrix 实例保留的内存空间)。然后选择分配的 100 个插槽之一,并将其​​值设置为指向已分配内存空间的指针。在您激活有效的参考之前,此内存一直可用。在这种情况下,在循环之后,arrRGB 会保持引用,因此您仍然可以访问在每个循环周期创建的原始 RGBMatrix。
    • 很抱歉打扰您,答案可能很明显,但这是我在 c# 中的第一个程序。所以这段代码有效,但是当循环完成并转到这段代码时:avrgR = +arrRGB[tempx].R;它给了我同样的 nullreferenceexception 错误。这是为什么呢?
    • 数组索引从 0 开始,以 Count-1 结束,因此您的 arrRGG[100] 声明应使用从 0 开始并以 99 结束的索引。
    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2012-03-30
    • 2023-03-08
    • 2016-06-20
    相关资源
    最近更新 更多