C# 在图片盒中绘制颜色?

C# 在图片盒中绘制颜色?,c#,graphics,drawing,picturebox,C#,Graphics,Drawing,Picturebox,在C#我有一个图片盒。我想画4种颜色。默认值为白色、红色、绿色和蓝色。我如何在这个画框中画出这4种颜色?或者我应该有4个picbox?在这种情况下,如何设置rgb颜色?向表单中添加一个PictureBox,为paint事件创建一个事件处理程序,并使其如下所示: private void PictureBox_Paint(object sender, PaintEventArgs e) { int width = myPictureBox.ClientSize.Width / 2;

在C#我有一个图片盒。我想画4种颜色。默认值为白色、红色、绿色和蓝色。我如何在这个画框中画出这4种颜色?或者我应该有4个picbox?在这种情况下,如何设置rgb颜色?

向表单中添加一个PictureBox,为paint事件创建一个事件处理程序,并使其如下所示:

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

这会将曲面划分为4个矩形,并将每个矩形绘制为白色、红色、绿色和蓝色。

向表单中添加一个PictureBox,为绘制事件创建一个事件处理程序,并使其如下所示:

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

这会将曲面分成4个矩形,并将每个矩形分别绘制为白色、红色、绿色和蓝色。

您需要指定具体要绘制的颜色。你不能画红色——那没有意义。但是,您可以在位置(0,0)处绘制一个高100像素、宽100像素的红色矩形。不过,我会尽我所能回答

如果要将形状的轮廓设置为特定颜色,则需要创建一个对象。但是,如果要使用颜色填充形状,则可以使用笔刷对象。下面是一个示例,演示如何绘制一个填充红色的矩形,以及一个用绿色勾勒的矩形:

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

您需要指定您特别想要绘制的内容。你不能画红色——那没有意义。但是,您可以在位置(0,0)处绘制一个高100像素、宽100像素的红色矩形。不过,我会尽我所能回答

如果要将形状的轮廓设置为特定颜色,则需要创建一个对象。但是,如果要使用颜色填充形状,则可以使用笔刷对象。下面是一个示例,演示如何绘制一个填充红色的矩形,以及一个用绿色勾勒的矩形:

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

如果要使用非预定义颜色,则需要从静态方法Color.FromArgb()获取颜色对象


向Oliver Hanappi致意如果您想使用非预定义的颜色,那么您需要从静态方法Color.FromArgb()获取颜色对象


向奥利弗·哈纳皮致意你的问题很模糊。是否要在每个部分中绘制矩形?画像素?什么?你的问题很模糊。是否要在每个部分中绘制矩形?画像素?什么?