Search code examples
c#system.drawing

I can not find the error in the code.a picture Box does not update


I'm creating a software in which pictureBox2 updates with the color of the pixel clicked on pictureBox1.

Already tried to use refresh(), but pictureBox2 does not change.

private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    Bitmap b = new Bitmap(pictureBox1.Image);
    color = b.GetPixel(e.X, e.Y); // Color

    solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb); //Image
    using (Graphics grp = Graphics.FromImage(solidColor))
    {
        SolidBrush co = new SolidBrush(color);
        grp.FillRectangle( co, 0, 0, pictureBox2.Width, pictureBox2.Height);
    }

    pictureBox2.Image = solidColor;
}

Solution

  • I was able to solve this by doing this.

    private void PictureBox1_Click(object sender, EventArgs e)
            {
    
                Bitmap b = new Bitmap(pictureBox1.Image);
                MouseEventArgs me = (MouseEventArgs)e;
                Point cord= me.Location;
                color = b.GetPixel(cord.X,cord.Y); 
    
                solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb);
                using (Graphics grp = Graphics.FromImage(solidColor))
                {
    
                    SolidBrush co = new SolidBrush(color);
                    grp.FillRectangle(co, 0, 0, pictureBox2.Width, pictureBox2.Height);
    
                }
    
                pictureBox2.Image = solidColor;
    
            }
    ```c#