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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1113437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:33:07  来源:igfitidea点击:

Drawing Colors in a picturebox?

c#graphicsdrawingpicturebox

提问by

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 picbox? in that case how do i set the rgb color?

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

采纳答案by IRBMe

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.

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

If you want to set the outline of a shape to a specific color, you would create a Penobject. 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:

如果要将形状的轮廓设置为特定颜色,则可以创建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));
}

回答by Fredrik M?rk

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

在表单中添加一个 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);
}

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

这会将表面分成 4 个矩形,并将每个矩形涂上白色、红色、绿色和蓝色。

回答by Oliver Hanappi

If you want to use non-predefined colors, then you need to get a Color object from the static method Color.FromArgb().

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

int r = 100;
int g = 200;
int b = 50;

Color c = Color.FromArgb(r, g, b);

Brush brush = new SolidBrush(c);
//...

Best Regards
Oliver Hanappi

最好的问候
奥利弗·哈纳皮