在图片框中绘制颜色?

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

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

如果要将形状的轮廓设置为特定颜色,则可以创建Pen对象。 但是,如果要使用颜色填充形状,则可以使用Brush对象。 这是一个如何绘制一个填充红色的矩形和一个绿色轮廓的矩形的示例:

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

将一个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个矩形,并以白色,红色,绿色和蓝色绘制每个矩形。

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

 int r = 100; int g = 200; int b = 50; Color c = Color.FromArgb(r, g, b); Brush brush = new SolidBrush(c); //... 

最好的祝福
奥利弗哈纳皮