創建繪圖表面
創建繪圖表面有兩種常用的方法下面設法得到PictureBox的繪圖表面
private void Form_Load(object sender SystemEventArgs e)
{
//得到pictureBox的繪圖表面
Graphics g = thispictureBoxCreateGraphics();
}
private void pictureBox_Paint(object sender SystemWindowsFormsPaintEventArgs e)
{
//得到pictureBox的繪圖表面
Graphics g = eGraphics;
}
可以利用Graphics對象繪制出各種圖形圖案控件的Paint事件和OnPaint方法都可以繪圖都是好時機在OnPaint方法裡繪制圖案一定從參數e裡面得到Graphics屬性下面是兩個例子
protected override void OnPaint(PaintEventArgs e)
{
eGraphicsClear(ColorWhite);
float x y w h;
x = thisLeft+;
y = thisTop+;
w = thisWidth;
h = thisHeight;
Pen pen = new Pen(ColorRed );
eGraphicsDrawRectangle(pen x y w h);
baseOnPaint (e);
}
private void PictureBoxII_Resize(object sender EventArgs e)
{
thisInvalidate();
}
private void button_Click(object sender SystemEventArgs e)
{
thispictureBoxIICreateGraphics()FillEllipse(
BrushesBlue );
}
和文本有關的三個類
FontFamily——定義有著相似的基本設計但在形式上有某些差異的一組字樣無法繼承此類
Font——定義特定的文本格式包括字體字號和字形屬性無法繼承此類
StringFormat——封裝文本布局信息(如對齊方式和行距)顯示操作(如省略號插入和國家標准 (National) 數字位替換)和 OpenType 功能無法繼承此類
下面的程序顯示了一段文字
private void button_Click(object sender SystemEventArgs e)
{
Graphics g = thispictureBoxIICreateGraphics();
gFillRectangle(BrushesWhite thispictureBoxIIClientRectangle);
string s = aaaaaaaaaaaaaaaaaaaaaaaaaa;
FontFamily fm = new FontFamily(ËÎÌå);
Font f = new Font(fm FontStyleBold GraphicsUnitPoint);
RectangleF rectF = new RectangleF( );
StringFormat sf = new StringFormat();
SolidBrush sbrush = new SolidBrush(ColorFromArgb( ));
sfLineAlignment = StringAlignmentCenter;
sfFormatFlags = StringFormatFlagsDirectionVertical;
gDrawString(s f sbrush rectF sf);
}
GDI+的路徑——GraphicsPath類
GraphicsPath類提供了一系列屬性和方法利用它可以獲取路徑上的關鍵點可以添加直線段圓等幾何元素可以獲得包圍矩形進行拾取測試這些功能都怎麼用要仔細看一下
private void button_Click(object sender SystemEventArgs e)
{
//繪圖表面
Graphics g = thispictureBoxIICreateGraphics();
//填充成白色
gFillRectangle(BrushesWhite thisClientRectangle);
//弄一個繪圖路徑¶
GraphicsPath gp = new GraphicsPath();
//添加一些集合圖形
gpAddEllipse( );
gpAddPie( );
gpAddRectangle(new Rectangle( ));
//在繪圖表面上繪制繪圖路徑
gDrawPath(PensBlue gp);
//平移
gTranslateTransform( );
//填充繪圖路徑¶
gFillPath(BrushesGreenYellow gp);
gpDispose();
}
區域——Region類
從已有的矩形和路徑可以創建Region使用GraphicsFillRegion方法繪制Region該類指示由矩形和由路徑構成的圖形形狀的內部無法繼承此類
漸變色填充
需要使用兩個刷子
線性梯度刷子(LinearGradientBrush)
路徑梯度刷子(PathGuadientBrush)
private void button_Click(object sender SystemEventArgs e)
{
//繪圖表面
Graphics g = thispictureBoxIICreateGraphics();
gFillRectangle(BrushesWhite thispictureBoxIIClientRectangle);
//定義一個線性梯度刷子
LinearGradientBrush lgbrush =
new LinearGradientBrush(
new Point( )
new Point( )
ColorFromArgb( )
ColorFromArgb( ));
Pen pen = new Pen(lgbrush);
//用線性筆刷梯度效果的筆繪制一條直線段並填充一個矩形
gDrawLine(pen );
gFillRectangle(lgbrush );
//定義路徑並添加一個橢圓
GraphicsPath gp = new GraphicsPath();
gpAddEllipse( );
//用該路徑定義路徑梯度刷子
PathGradientBrush brush =
new PathGradientBrush(gp);
//顏色數組
Color[] colors = {
ColorFromArgb( )
ColorFromArgb( )
ColorFromArgb( )
ColorFromArgb( )};
//定義顏色漸變比率
float[] r = {f f f f};
ColorBlend blend = new ColorBlend();
blendColors = colors;
blendPositions = r;
brushInterpolationColors = blend;
//在橢圓外填充一個矩形
gFillRectangle(brush );
//用添加了橢圓的路徑定義第二個路徑梯度刷子
GraphicsPath gp = new GraphicsPath();
gpAddEllipse( );
PathGradientBrush brush = new PathGradientBrush(gp);
//設置中心點位置和顏色
brushCenterPoint = new PointF( );
brushCenterColor = ColorFromArgb( );
//設置邊界顏色
Color[] color = {ColorFromArgb( )};
brushSurroundColors = color;
//用第二個梯度刷填充橢圓
gFillEllipse(brush );
}
GDI+的坐標系統
通用坐標系——用戶自定義坐標系
頁面坐標系——虛擬坐標系
設備坐標系——屏幕坐標系
當頁面坐標系和設備坐標系的單位都是象素時它們相同
private void button_Click(object sender SystemEventArgs e)
{
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
thisDraw(g);
}
private void Draw(Graphics g)
{
gDrawLine(PensBlack );
gDrawEllipse(PensBlack );
gDrawArc(PensBlack );
gDrawRectangle(PensGreen );
}
private void button_Click(object sender SystemEventArgs e)
{
//左移
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
gTranslateTransform( );
thisDraw(g);
}
private void button_Click(object sender SystemEventArgs e)
{
//右移
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
gTranslateTransform( );
thisDraw(g);
}
private void button_Click(object sender SystemEventArgs e)
{
//旋轉
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
gRotateTransform();
thisDraw(g);
}
private void button_Click(object sender SystemEventArgs e)
{
//放大
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
gScaleTransform(f f);
thisDraw(g);
}
private void button_Click(object sender SystemEventArgs e)
{
//縮小
Graphics g = thispictureBoxIICreateGraphics();
gClear(ColorWhite);
gScaleTransform(f f);
thisDraw(g);
}
全局坐標——變換對於繪圖表面上的每個圖元都會產生影響通常用於設定通用坐標系
一下程序將原定移動到控件中心並且Y軸正向朝上
//先畫一個圓
Graphics g = eGraphics;
gFillRectangle(BrushesWhite thisClientRectangle);
gDrawEllipse(PensBlack );
//使y軸正向朝上必須做相對於x軸鏡像
//變換矩陣為[]
Matrix mat = new Matrix( );
gTransform = mat;
Rectangle rect = thisClientRectangle;
int w = rectWidth;
int h = rectHeight;
gTranslateTransform(w/ h/);
//以原點為中心做一個半徑為的圓
gDrawEllipse(PensRed );
gTranslateTransform( );
gDrawEllipse(PensGreen );
gScaleTransform( );
gDrawEllipse(PensBlue );
局部坐標系——只對某些圖形進行變換而其它圖形元素不變
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = eGraphics;
//客戶區設置為白色
gFillRectangle(BrushesWhite thisClientRectangle);
//y軸朝上
Matrix mat = new Matrix( );
gTransform = mat;
//移動坐標原點到窗體中心
Rectangle rect = thisClientRectangle;
int w = rectWidth;
int h = rectHeight;
gTranslateTransform(w/ h/);
//在全局坐標下繪制橢圓
gDrawEllipse(PensRed );
gFillRectangle(BrushesBlack );
gFillRectangle(BrushesBlack );
gFillRectangle(BrushesBlack );
gFillRectangle(BrushesBlack );
//創建一個橢圓然後在局部坐標系中進行變換
GraphicsPath gp = new GraphicsPath();
gpAddEllipse( );
Matrix mat = new Matrix();
//平移
matTranslate( );
//旋轉
matRotate();
gpTransform(mat);
gDrawPath(PensBlue gp);
PointF[] p = gpPathPoints;
gFillRectangle(BrushesBlack p[]X p[]Y+ );
gFillRectangle(BrushesBlack p[]X p[]Y+ );
gFillRectangle(BrushesBlack p[]X p[]Y );
gFillRectangle(BrushesBlack p[]X p[]Y );
gpDispose();
//baseOnPaint (e);
}
Alpha混合
ColorFromArgb()的A就是AlphaAlpha的取值范圍從到表示完全透明完全不透明
當前色=前景色×alpha/+背景色×(-alpha)/
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = eGraphics;
//創建一個填充矩形
SolidBrush brush = new SolidBrush(ColorBlueViolet);
gFillRectangle(brush );
//創建一個位圖其中兩個位圖之間有透明效果
Bitmap bm = new Bitmap( );
Graphics bg = GraphicsFromImage(bm);
SolidBrush redBrush =
new SolidBrush(ColorFromArgb( ));
SolidBrush greenBrush =
new SolidBrush(ColorFromArgb( ));
bgFillRectangle(redBrush );
bgFillRectangle(greenBrush );
gDrawImage(bm );
//創建一個位圖其中兩個位圖之間沒有透明效果
Bitmap bm = new Bitmap( );
Graphics bg = GraphicsFromImage(bm);
bgCompositingMode = CompositingModeSourceCopy;
bgFillRectangle(redBrush );
bgFillRectangle(greenBrush );
gCompositingQuality = CompositingQualityGammaCorrected;
gDrawImage(bm );
//baseOnPaint (e);
}
反走樣
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = eGraphics;
//放大倍
gScaleTransform( );
//沒有反走樣的圖形和文字
Draw(g);
//設置反走樣
gSmoothingMode = SmoothingModeAntiAlias;
//右移
gTranslateTransform( );
//再繪制就是反走樣之後的了
Draw(g);
//baseOnPaint (e);
}
private void Draw(Graphics g)
{
//繪制圖形和文字
gDrawLine(PensGray );
gDrawEllipse(PensGray );
string s = 反走樣測試;
Font font = new Font(宋體 );
SolidBrush brush = new SolidBrush(ColorGray);
gDrawString(s font brush );
}
完了暫時先總結那麼多以後發現必要的可以再補充
From:http://tw.wingwit.com/Article/program/net/201311/12222.html