5、漸層色填充
需要使用兩個刷子:
線性梯度刷子(LinearGradientBrush)
路徑梯度刷子(PathGuadientBrush)
private void button4_Click(object sender,System.EventArgs e)
{
//繪圖表面
Graphics 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 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 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 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)
{
//左移
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(object sender, System.EventArgs e)
{
//旋轉
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.RotateTransform(-30);
this.Draw(g);
}
private void button8_Click(object sender, System.EventArgs e)
{
//放大
Graphics 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)
{
//縮小
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);
//在全域座標下繪製橢圓
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();
//平移
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、Alpha混合
Color.FromArgb()的A就是Alpha。Alpha的取值範圍從0到255。0表示完全透明,255完全不透明。
當前色=前景色彩×alpha/255+背景色×(255-alpha)/255
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//建立一個填充矩形
SolidBrush brush = new SolidBrush(Color.BlueViolet);
g.FillRectangle(brush, 180, 70, 200, 150);
//建立一個位元影像,其中兩個位元影像之間有透明效果
Bitmap 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);
//建立一個位元影像,其中兩個位元影像之間沒有透明效果
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.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 brush = new SolidBrush(Color.Gray);
g.DrawString(s, font, brush, 10, 40);
}