代码之家  ›  专栏  ›  技术社区  ›  user3033953

从图像中提取RGB信息到阵列中

  •  0
  • user3033953  · 技术社区  · 9 年前

    我必须保存一个位图文件,然后访问每个像素的rgb,并将每个像素颜色(即红色、绿色和蓝色)的十进制代码保存在一个单独的矩阵(数组)中。

    为了在bmp图像中隐藏文本文件,我需要将每个rgb代码保存在单独的矩阵中…我正在寻找有效的方法,这是我的代码

    此代码将运行,但我无法在标签文本中显示每个矩阵的结果。如何查看每个矩阵的输出?

     Bitmap bmp1 = new Bitmap(@"D:a.jpg");
            pictureBox1.Image = bmp1;
            Color col = new Color();
            int w = Int32.Parse(bmp1.Width.ToString());
            int h = Int32.Parse(bmp1.Height.ToString()); 
            int[,] redstr = new int[w,h];
            int[,] greenstr = new int[w, h];
            int[,] bluestr = new int[w, h];
            int red = 0, green = 0, blue = 0;
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    col = bmp1.GetPixel(i, j);
                    red = col.R;
                    green = col.G;
                    blue = col.B;
                    redstr[i, j] = red;
                    greenstr[i, j] = green;
                    bluestr[i, j] = blue;
    
                }
    
                }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   3-14159265358979323846264    9 年前

    从下一页。。。 https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx

    您可以使用类似的方法来访问图像的每个像素。。。

    private void GetPixel_Example(PaintEventArgs e)
    {
    
    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("YOURFILENAME.jpg");
    
    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);
    
    }
    

    然后您可以检查RGB组件的pixelColour。

    显然,您需要创建一个与图像尺寸相同的2D阵列(矩阵)。如果你需要存储单独的RGB组件,那么你需要一个3D阵列。