C# - 包含图形的PictureBox中的像素颜色计数

时间:2016-04-06 14:33:01

标签: c# graphics bitmap picturebox

早上好StackOverflow!

我有一个小项目,可以生成一些G代码,带有一个小型可视化工具,可以看到G代码将在机器上重新创建。

我正在尝试计算PictureBox中的所有像素,以计算PictureBox中红色像素的百分比。

虽然我很清楚如何自己计算像素,但我无法得到我的PictureBox所显示的内容。

Bitmap bmp = new Bitmap(PB_Graph.Width, PB_Graph.Height, graphics);
        int redColor = 0;
        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                Color color = bmp.GetPixel(x, y);

                if (color.ToArgb() == Color.Red.ToArgb())
                    redColor++;
            }
        }
        Double Coverage = redColor * 100 / (PB_Graph.Width * PB_Graph.Height);

我创建了一个图形,但是图形是全局初始化的,因为我的许多函数都写入了它。

Graphics graphics;
    public GCodeRandomizer()
    {
        InitializeComponent();
        graphics = PB_Graph.CreateGraphics();
        graphics.ScaleTransform(1.0F, -1.0F);
        graphics.TranslateTransform(0.0F, -(float)PB_Graph.Height);
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
    }

DrawLine的例子:

            graphics.DrawLine(new Pen(Color.Red), (float)((TempX - XBase) * PB_Graph.Width / Double.Parse(TBX_BufferZone_Width.Text)), (float)((TempY - YBase - 4) * PB_Graph.Height / Double.Parse(TBX_BufferZone_Height.Text)), (float)((X - XBase) * PB_Graph.Width / Double.Parse(TBX_BufferZone_Width.Text)), (float)((Y - YBase - 4) * PB_Graph.Height / Double.Parse(TBX_BufferZone_Height.Text)));

我的程序是什么样的: MyProject

所以,我需要的是一种将我的图形转换为位图的方法,因为我知道这是计算像素所需要的。 我的问题是:当我执行Bitmap bmp = new Bitmap(PB_Graph.Width, PB_Graph.Height, graphics);之类的操作时,它只显示没有我的绘图的空白图像。

谢谢!

0 个答案:

没有答案