C# 2 での GDI+ プログラミングのための 10 の基本スキル

高洛峰
リリース: 2016-12-17 09:53:12
オリジナル
1039 人が閲覧しました

5. グラデーションカラー塗りつぶし

次の 2 つのブラシを使用する必要があります:

LinearGradientBrush(LinearGradientBrush)

PathGuadientBrush(PathGuadientBrush)

private void button4_Click(object sender,System.EventArgs e)

{

/ /描画面Graphics g = this.pictureboxii1.creategraphics();

new Point(0, 10),

new Point(150, 10),

Color.FromArgb(255, 0, 0),

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

ペンペン= 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 Brush =

new PathGradientBrush(gp);

//カラー配列

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.3f, 0.6f, 1.0f};

ColorBlend ブレンド = new ColorBlend();

blend.Colors = Colors;

blend.Positions = r;

brush .InterpolationColors = Blend;

//楕円の外側の四角形を塗りつぶします

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

//楕円に追加されたパスで 2 番目のパス グラデーション ブラシを定義します

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;

//2 番目のグラデーション ブラシを使用して楕円を塗りつぶします

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

}

6、GDI+ 座標系

ユニバーサル座標系 - ユーザー定義の座標系。

ページ座標系 - 仮想座標系。

デバイス座標系 - 画面座標系。

ページ座標系とデバイス座標系の単位が両方ともピクセルの場合、それらは同じです。


private void button10_Click(object sender, System.EventArgse)

{

Graphics g = this.pictureBoxII1.CreateGraphics();


g.Clear(Color.White);

this.Draw(g);

}

private void Draw(Graphics 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 button5_Click(object sender, System.EventArgs e)

{

//Shift left

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g。 TranslateTransform(-50, 0);

this.Draw(g);

}

private void button6_Click(object sender, System.EventArgs e)

{

//右にシフト

Graphics g = this .pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(50, 0);

this.Draw(g);

}

private void button7_Click(オブジェクト送信者, System.EventArgs e)

{

//

グラフィックスを回転 g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.RotateTransform(-30);

this.Draw(g);

}

private void button8_Click(object sender, System.EventArgs e)

{

//拡大

グラフィックス g = this.pictureBoxII1.CreateGraphics();

g。 Clear(Color.White);

g.ScaleTransform(1.2f, 1.2f);

this.Draw(g);

}

private void button9_Click(object sender, System.EventArgs e)

{

//Reduce

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.ScaleTransform(0.8f, 0.8f);

this.Draw(g) ;

}

7. グローバル座標 - 変換は描画面上のすべてのプリミティブに影響を与えます。通常、ユニバーサル座標系を設定するために使用されます。

次のプログラムは、元の固定点をコントロールの中心に移動し、Y 軸が上を向くようにします。

//まず円を描きます

Graphics 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;

Rectangle rect = this.ClientRectangle;

int w = rect.Width;

int h = rect.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;

//座標原点を中心に移動しますフォーム

Rectangle rect = this.ClientRectangle;

int w = rect.Width;

int h = rect.Height;

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

//Drawグローバル座標の楕円

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

//Rotation

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. アルファブレンディング


Color.FromArgb() の A は Alpha です。アルファ値の範囲は 0 ~ 255 です。0 は完全に透明を意味し、255 は完全に不透明を意味します。


現在の色 = 前景色×アルファ/255+背景色×(255-アルファ)/255


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//塗りつぶされた長方形を作成します

SolidBrush Brush = new SolidBrush(Color.BlueViolet);

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

// 2 つのビットマップの間にビットマップを作成します 透明効果があります

ビットマップbm1 = new Bitmap(200, 100);

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

//2 つのビットマップ間に透明効果のないビットマップを作成します

Bitmap bm2 = new Bitmap( 200, 100); aGraphics BG2 = Graphics。 fromimage (BM2); gBg2.fillrectangle ( greenBrush, 30, 30, 150, 70);

g.CompositingQuality = CompositingQuality.GammaCorrected;

g.DrawImage(bm2, 300, 200);

/ /base.OnPaint (e);

}

10. アンチエイリアス


protected override void OnPaint(PaintEventArgs e)

{

Graphics 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 = "アンチエイリアシング テスト" ;

Font font = new Font("宋体", 5);

SolidBrush ブラシ = new SolidBrush(Color.Gray);

g。 DrawString(s, font, Brush, 10, 40);

}


C# 2 での GDI+ プログラミングの 10 の基本スキルについては、関連記事については、PHP 中国語 Web サイトに注目してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!