Drawing Colors in a picturebox?

后端 未结 3 1212
醉梦人生
醉梦人生 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 19:01

    You need to specify what it is you would specifically like to draw. You can't draw a red - that makes no sense. You can, however, draw a red rectangle at location (0,0) which is 100 pixels tall and 100 wide. I will answer what I can, however.

    If you want to set the outline of a shape to a specific color, you would create a Pen object. If you want to fill a shape with a color, however, then you would use a Brush object. Here's an example of how you would draw a rectangle filled with red, and a rectangle outlined in green:

    private void pictureBox_Paint(object sender, PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
    
        Brush brush = new SolidBrush(Color.Red);
        graphics.FillRectangle(brush, new Rectangle(10, 10, 100, 100));
    
        Pen pen = new Pen(Color.Green);
        graphics.DrawRectangle(pen, new Rectangle(5, 5, 100, 100));
    }
    

提交回复
热议问题