>  기사  >  백엔드 개발  >  C# 2의 GDI+ 프로그래밍을 위한 10가지 기본 기술

C# 2의 GDI+ 프로그래밍을 위한 10가지 기본 기술

高洛峰
高洛峰원래의
2016-12-17 09:53:121024검색

5. 그라데이션 색상 채우기

두 개의 브러시를 사용해야 합니다:

LinearGradientBrush(LinearGradientBrush)

PathGuadientBrush(PathGuadientBrush)

private void 버튼4_클릭 (객체 전송자,System.EventArgs e)

{

//도면 표면

그래픽 g =this.pictureBoxII1.CreateGraphics();

g .FillRectangle(Brushes.White,this.pictureBoxII1.ClientRectangle);

//선형 그래디언트 브러시 정의

LinearGradientBrush lgbrush =

new LinearGradientBrush(

new Point(0, 10),

new Point(150, 10),

Color.FromArgb(255, 0, 0),

Color .FromArgb( 0, 255, 0));

Pen pen = new Pen(lgbrush);


//선형 브러시 그라데이션 효과 펜으로 직선 선분을 그리고 직사각형을 채웁니다

g.DrawLine(pen, 10, 130, 500, 130);

g.FillRectangle(lgbrush, 10, 150, 370, 30);


//경로 정의 및 타원 추가

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(10, 10, 200, 100);

// 이 경로를 사용하여 경로 그라데이션 브러시

PathGradientBrush 브러시 =

new PathGradientBrush(gp);

//Color array

Color[] colors = {

Color.FromArgb(255, 0, 0),

Color.FromArgb(100, 100, 100),

Color.FromArgb(0, 255, 0),

Color.FromArgb(0, 0, 255)};

//색상 그라데이션 비율 정의

float[] r = {0.0f, 0.3 f , 0.6f, 1.0f};

ColorBlend blend = new ColorBlend();

blend.Colors = colors;

blend.Positions = r;

brush.InterpolationColors = blend;

//타원 외부의 직사각형 채우기

g.FillRectangle(brush, 0, 0, 210, 110);


//타원이 추가된 경로로 두 번째 경로 그라데이션 브러시 정의

GraphicsPath gp2 = new GraphicsPath();

gp2.AddEllipse(300, 0, 200, 100) ;

PathGradientBrush Brush2 = new PathGradientBrush(gp2);

//중심점 위치 및 색상 설정

brush2.CenterPoint = new PointF(450, 50);

brush2.CenterColor = Color.FromArgb(0, 255, 0);

//테두리 색상 설정

Color[] color2 = {Color.FromArgb(255, 0, 0)};

brush2.SurroundColors = color2;

//두 번째 그라데이션 브러시로 타원을 채웁니다

g.FillEllipse(brush2, 300, 0, 200, 100);

}


6. GDI+ 좌표계


범용 좌표계 - 사용자 정의 좌표계입니다.

페이지 좌표계 - 가상 좌표계.
기기 좌표계 - 화면 좌표계.


페이지 좌표계와 디바이스 좌표계의 단위가 모두 픽셀인 경우에는 동일합니다.

private void 버튼10_Click(객체 전송자, System.EventArgse)

{

그래픽 g = this.pictureBoxII1.CreateGraphics();

g.Clear (Color.White);

this.Draw(g);

}


private void Draw(그래픽 g)

{

g.DrawLine(Pens.Black, 10, 10, 100, 100);

g.DrawEllipse(Pens.Black, 50, 50, 200, 100);

g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);

g.DrawRectangle(Pens.Green, 50, 200, 150, 100);

}


private void 버튼5_Click(객체 전송자, System.EventArgs e)

{

//왼쪽으로 이동

그래픽 g = this .pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(-50, 0);

this.Draw(g) ;

}


private void 버튼6_Click(object sender, System.EventArgs e)

{

//오른쪽으로 이동

그래픽 g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(50, 0);

this .Draw(g);

}


private void 버튼7_Click(객체 전송자, System.EventArgs e)

{

//회전

그래픽 g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.RotateTransform(-30);

this.Draw(g);

}


private void 버튼8_Click(객체 전송자, System.EventArgs e)

{

//확대

그래픽 g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.ScaleTransform(1.2f, 1.2f );

this.Draw(g);

}


private void 버튼9_Click(객체 전송자, System.EventArgs e)

{

//축소

그래픽 g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g. ScaleTransform (0.8f, 0.8f);

this.Draw(g);

}


7. 전역 좌표 - 도면의 각 점에 대한 변환 표면 모든 그래픽 요소가 영향을 받습니다. 일반적으로 범용 좌표계를 설정하는 데 사용됩니다.

다음 프로그램은 Y축이 위쪽을 향하도록 하여 원래 고정점을 제어 중심으로 이동합니다.

//먼저 원 그리기

그래픽 g = e.Graphics;

g.FillRectangle(Brushes.White, this.ClientRectangle);

g.DrawEllipse(Pens.Black, -100, -100, 200, 200);


//y축을 위쪽으로 양수로 만들려면 x축을 기준으로 미러링해야 합니다.

//변환 행렬은 ​​[1,0,0,-1,0,0]

Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);

g.Transform = mat;

직사각형 ect = this.ClientRectangle;

int w = ret.Width;

int h = ret.Height;

g.TranslateTransform(w/2, -h/2);


//원점을 중심으로 반지름이 100인 원을 만듭니다.

g.DrawEllipse( Pens.Red, -100, -100, 200, 200);
g.TranslateTransform(100, 100);

g.DrawEllipse(Pens.Green, -100, -100, 200, 200);

g.ScaleTransform(2, 2);

g.DrawEllipse(Pens.Blue , -100, -100, 200, 200);

8. 로컬 좌표계 - 특정 그래픽만 변환되고 다른 그래픽 요소는 변경되지 않습니다.


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//클라이언트 영역이 설정되었습니다. 흰색으로

g.FillRectangle(Brushes.White, this.ClientRectangle);

//y축이 위를 향합니다

Matrix mat = new Matrix(1, 0, 0, - 1, 0, 0);

g.Transform = mat;

//좌표 원점을 양식 중앙으로 이동

직사각형 직사각형 = this.ClientRectangle;

int w = ret.Width;

int h = ret.Height;

g.TranslateTransform(w/2, -h/2) ;

/ /전역 좌표로 타원 그리기

g.DrawEllipse(Pens.Red, -100, -100, 200, 200);

g.FillRectangle( Brushes.Black, -108, 0, 8, 8);

g.FillRectangle(Brushes.Black, 100, 0, 8, 8);

g.FillRectangle(Brushes.Black , 0, 100, 8, 8 );

g.FillRectangle(Brushes.Black, 0, -108, 8, 8);


//타원을 만들고 변환 로컬 좌표계

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(-100, -100, 200, 200);

Matrix mat2 = new Matrix();

//Translation

mat2.Translate(150, 150);

//회전

mat2.Rotate(30) ;

gp .Transform(mat2);

g.DrawPath(Pens.Blue, gp);


PointF[] p = gp.PathPoints;

g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[3].X-2 , p[3].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[6].X-4, p[6] .Y-4, 4, 4 );

g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);
gp.Dispose();
//base.OnPaint (e);

}


9. 알파 블렌딩


A in Color .FromArgb()는 알파입니다. 알파 값의 범위는 0부터 255까지입니다. 0은 완전히 투명함을 의미하고 255는 완전히 불투명함을 의미합니다.


현재 색상 = 전경색×알파/255+배경색×(255-알파)/255


protected override void OnPaint(PaintEventArgs e)

{

그래픽 g = e.Graphics;

//채워진 직사각형 만들기

SolidBrush 브러시 = new SolidBrush(Color.BlueViolet);

g.FillRectangle(brush, 180, 70, 200, 150);

//두 비트맵 사이에 투명도 효과를 적용한 비트맵 생성

Bitmap bm1 = new Bitmap (200, 100 );

그래픽 bg1 = Graphics.FromImage(bm1);

SolidBrush redBrush =

new SolidBrush(Color.FromArgb(210, 255, 0, 0));

SolidBrush greenBrush =

new SolidBrush(Color.FromArgb(210, 0, 255, 0));

bg1.FillRectangle(redBrush, 0, 0, 150, 70);

bg1.FillRectangle(greenBrush, 30, 30, 150, 70);

g.DrawImage(bm1, 100, 100);

//생성 두 비트맵 사이에 투명 효과가 없는 비트맵

Bitmap bm2 = new Bitmap(200, 100);

Graphics bg2 = Graphics.FromImage(bm2) ;

bg2 .CompositingMode = CompositingMode.SourceCopy;

bg2.FillRectangle(redBrush, 0, 0, 150, 170);

bg2.FillRectangle(greenBrush, 30, 30, 150, 70);

g.CompositingQuality = CompositingQuality.GammaCorlected;

g.DrawImage(bm2, 300, 200);
//base.OnPaint (e)

}

10. 앤티앨리어싱


protected override void OnPaint(PaintEventArgs e)

{

그래픽 g = e .Graphics;

//8배 확대

g.ScaleTransform(8, 8);

//앤티앨리어싱 및 텍스트가 없는 그래픽

Draw(g);


//안티앨리어싱 설정

g.SmoothingMode = SmoothingMode.AntiAlias;


/ /오른쪽으로 40시프트

g.TranslateTransform(40, 0);

//다음 그림은 앤티앨리어싱 후입니다.

Draw(g);
/ /base.OnPaint (e );

}
private void Draw(Graphics g)

{

//그래픽 및 텍스트 그리기

g.DrawLine(Pens. Gray, 10, 10, 40, 20);

g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);

string s = " 앤티앨리어싱 테스트";

글꼴 글꼴 = new Font("宋体", 5);

SolidBrush 브러시 = new SolidBrush(Color.Gray);


g.DrawString(s , 글꼴, 브러시, 10, 40);

}



C# 자세히 알아보기 GDI+ 프로그래밍의 10가지 기본 기술 2. 관련 기사는 PHP 중국어 홈페이지를 참고해주세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.