文章詳細講解C#畫圖的模式與縮放功能。實體建模軟體中,可以獨立的設定並儲存各種座標系,並隨時調用。
在實體建模軟體中,經常有設定並儲存各種參考座標系的功能,方便建立模型。C#畫圖中也有這種類似功能。不過沒有建模軟體那麼強大。實體建模軟體中,可以獨立的設定並儲存各種座標系,並隨時調用。而這裡只能以嵌套的形式調用,當返回到上一級狀態時,跳過的狀態就不再儲存了。
C#畫圖普通模式主要命令:
- state = graphics.BeginContainer(); 建一個新繪圖狀態
- e.Graphics.EndContainer(state1); 結束這個繪圖狀態
- Rectangle rect = new Rectangle(0, 0, 100, 100);//樣本圖形
-
- GraphicsContainer state1 =
- e.Graphics.BeginContainer(); //建一個新繪圖座標state1
- e.Graphics.TranslateTransform(100, 100);
- //移動座標繫到100,100,畫藍色矩形標記
- e.Graphics.DrawRectangle(Pens.Blue, rect);
-
- GraphicsContainer state2 =
- e.Graphics.BeginContainer(); //在此基礎上建一個繪圖座標state2
- e.Graphics.RotateTransform(45);//旋轉45度, 畫紅色矩形標記
- e.Graphics.DrawRectangle(Pens.Red, rect);
- e.Graphics.TranslateTransform(100, 100);
- e.Graphics.DrawRectangle(Pens.Black, rect);
- e.Graphics.EndContainer(state2);//退出座標系2, 畫藍橢圓
-
- e.Graphics.DrawEllipse(Pens.Blue, rect);
- e.Graphics.EndContainer(state1);//退出state1, 畫紅橢圓
- e.Graphics.DrawRectangle(Pens.Red, rect);
建立狀態1
移動到100,100,畫藍色矩形
建被嵌套的狀態2
移動到200,0,畫紅色矩形
退出狀態2,畫藍色橢圓
退出狀態1,畫紅色矩形
狀態2是被嵌套的,如果直接退出狀態1畫紅色矩形,狀態2不再被儲存。
graphics.BeginContainer()和EndGontainer是儲存和返回當前畫板狀態,當然,移動只是一種改變畫板狀態的方式。
C#畫圖縮放功能主要命令:
- GraphicsContainer containerState=
- e.Graphics.BeginContainer( destRect,srcRect, GraphicsUnit.Pixel);
-
- // 多加兩個參數,destRect和scrRect制定縮放大小 Pixel指定單位
- Rectangle srcRect=newRectangle( 0,0,200,200);
- Rectangle destRect=newRectangle( 200,200,100,100); //建一個比例縮放的畫圖板.
- GraphicsContainer containerState= e.Graphics.BeginContainer(
- destRect,srcRect, GraphicsUnit.Pixel); //繪圖縮放綠矩形.
- e.Graphics.FillRectangle( newSolidBrush(Color.Red),0,0,100,100); //退出此繪圖板.
- e.Graphics.EndContainer(containerState); //繪原始紅矩形.
- e.Graphics.FillRectangle( newSolidBrush(Color.Green),0,0,100,100);
以上就介紹了C#畫圖的模式與縮放功能。