C# - Pixel Color Count in PictureBox with Graphics -


good morning stackoverflow!

i have small project generates g code, small visualizer see g code recreate on machine.

i trying count pixels in picturebox calculate percentage of red pixels in picturebox.

while have idea how calculate pixels themselves, cannot picturebox showing.

bitmap bmp = new bitmap(pb_graph.width, pb_graph.height, graphics);         int redcolor = 0;         (int x = 0; x < bmp.width; x++)         {             (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); 

i created graphics go it, graphic initialized globally, since many of functions write it.

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

exemple of 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))); 

what program looks like: myproject

so, need way convert graphics bitmap, because know needed count pixels. problem is: when bitmap bmp = new bitmap(pb_graph.width, pb_graph.height, graphics);, shows blank image without drawings.

thanks!


Comments