Drawing Colors in a picturebox?

后端 未结 3 1239
醉梦人生
醉梦人生 2021-01-25 18:22

In C# i have a picturebox. i would like to draw 4 colors. The default will be white, red, green, blue. How do i draw these 4 colors stritched in this picbox? or should i have 4

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-25 18:51

    Add a PictureBox to the form, create an event handler for the paint event, and make it look like this:

    private void PictureBox_Paint(object sender, PaintEventArgs e)
    {
        int width = myPictureBox.ClientSize.Width / 2;
        int height = myPictureBox.ClientSize.Height / 2;
    
        Rectangle rect = new Rectangle(0, 0, width, height);
        e.Graphics.FillRectangle(Brushes.White, rect);
        rect = new Rectangle(width, 0, width, height);
        e.Graphics.FillRectangle(Brushes.Red, rect);
        rect = new Rectangle(0, height, width, height);
        e.Graphics.FillRectangle(Brushes.Green, rect);
        rect = new Rectangle(width, height, width, height);
        e.Graphics.FillRectangle(Brushes.Blue, rect);
    }
    

    This will divide the surface into 4 rectangles and paint each of them in the colors White, Red, Green and Blue.

提交回复
热议问题