문제

C#에는 그림 상자가 있습니다. 4 가지 색상을 그리고 싶습니다. 기본값은 흰색, 빨간색, 녹색, 파란색입니다. 이 Picbox 에서이 4 가지 색상을 묶는 방법은 무엇입니까? 아니면 4 개의 picbox가 있어야합니까? 이 경우 RGB 색상을 어떻게 설정합니까?

도움이 되었습니까?

해결책

구체적으로 그리는 것이 무엇인지 지정해야합니다. 당신은 빨간색을 그릴 수 없습니다 - 그것은 말이되지 않습니다. 그러나 높이 100 픽셀, 폭 100 픽셀 인 위치 (0,0)에서 빨간 사각형을 그릴 수 있습니다. 그러나 나는 내가 할 수있는 것에 대답 할 것입니다.

모양의 개요를 특정 색상으로 설정하려면 물체. 그러나 색상으로 모양을 채우려면 브러시 객체를 사용합니다. 다음은 빨간색으로 채워진 사각형과 녹색으로 설명 된 사각형을 어떻게 그리는 지에 대한 예입니다.

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

다른 팁

그림 상자를 양식에 추가하고 페인트 이벤트를위한 이벤트 핸들러를 만들고 다음과 같이 보이게하십시오.

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 개의 사각형으로 나누고 각각 흰색, 빨간색, 녹색 및 파란색으로 페인트합니다.

정의되지 않은 색상을 사용하려면 정적 메소드 색상에서 색상 객체를 가져와야합니다 .FromArgb ().

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

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

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

친애하는
올리버 한나피

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top