SKCanvas 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
封裝了所有關於繪製到裝置(點陣圖或表面)的狀態。
public class SKCanvas : SkiaSharp.SKObject
- 繼承
- 衍生
範例
var info = new SKImageInfo(640, 480);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// set up drawing tools
var paint = new SKPaint {
IsAntialias = true,
Color = new SKColor(0x2c, 0x3e, 0x50),
StrokeCap = SKStrokeCap.Round
};
// create the Xamagon path
var path = new SKPath();
path.MoveTo(71.4311121f, 56f);
path.CubicTo(68.6763107f, 56.0058575f, 65.9796704f, 57.5737917f, 64.5928855f, 59.965729f);
path.LineTo(43.0238921f, 97.5342563f);
path.CubicTo(41.6587026f, 99.9325978f, 41.6587026f, 103.067402f, 43.0238921f, 105.465744f);
path.LineTo(64.5928855f, 143.034271f);
path.CubicTo(65.9798162f, 145.426228f, 68.6763107f, 146.994582f, 71.4311121f, 147f);
path.LineTo(114.568946f, 147f);
path.CubicTo(117.323748f, 146.994143f, 120.020241f, 145.426228f, 121.407172f, 143.034271f);
path.LineTo(142.976161f, 105.465744f);
path.CubicTo(144.34135f, 103.067402f, 144.341209f, 99.9325978f, 142.976161f, 97.5342563f);
path.LineTo(121.407172f, 59.965729f);
path.CubicTo(120.020241f, 57.5737917f, 117.323748f, 56.0054182f, 114.568946f, 56f);
path.LineTo(71.4311121f, 56f);
path.Close();
// draw the Xamagon path
canvas.DrawPath(path, paint);
}
備註
畫布封裝了所有關於繪製到裝置(點陣圖或曲面)的狀態。
這包括對裝置本身的參考,以及一堆矩陣/剪輯值。 對於任何給定的繪圖呼叫(例如 DrawRect),被繪製物件的幾何體會透過堆疊中所有矩陣的串接而轉換。 轉換後的幾何體會被堆疊中所有剪裁的交叉點所裁剪。
畫布保存繪圖裝置的狀態,而被繪製物件的狀態(風格)則由顏料持有,顏料作為每種「繪製」方法的參數。 Paint 會儲存顏色、字體、文字大小、筆劃寬度、著色器(例如漸層、圖案)等屬性。
當進入 SKSurface.Canvas 某個表面的屬性時,畫布會被返回。
建築
SkiaSharp 有多個後端,這些後端可接收 SKCanvas 繪製指令,包括:
- 光柵 Surface
- GPU Surface
- PDF 文件
- XPS 文件 (實驗性)
- SVG Canvas (實驗性)
- 圖片
- Null Canvas (用於測試)
構建光柵 Surface
光柵後端會繪製到一個記憶體區塊。 這些記憶體可由 SkiaSharp 或用戶端管理。
為 Raster 和 Ganesh 後端建立畫布的推薦方法是使用 SKSurface一個物件,這是一個管理畫布指令被繪製到記憶體中的物件。
// define the surface properties
var info = new SKImageInfo(256, 256);
// construct a new surface
var surface = SKSurface.Create(info);
// get the canvas from the surface
var canvas = surface.Canvas;
// draw on the canvas ...
或者,我們也可以明確指定 Surface 的記憶體,而不是請 SkiaSharp 來管理。
// define the surface properties
var info = new SKImageInfo(256, 256);
// allocate memory
var memory = Marshal.AllocCoTaskMem(info.BytesSize);
// construct a surface around the existing memory
var surface = SKSurface.Create(info, memory, info.RowBytes);
// get the canvas from the surface
var canvas = surface.Canvas;
// draw on the canvas ...
建構 GPU Surface
GPU 表面必須有一個 GRContext 物件來管理 GPU 上下文,以及相關的材質和字型快取。
GRContext 物件會與 OpenGL 上下文或 Vulkan 裝置一一匹配。 也就是說,所有 SKSurface 將被渲染成相同 OpenGL 情境或 Vulkan 裝置的實例,都應該共享一個 GRContext.
SkiaSharp 不會幫你建立 OpenGL 情境或 Vulkan 裝置。 在 OpenGL 模式下,當 SkiaSharp 呼叫時,也假設正確的 OpenGL 上下文已與當前執行緒保持最新。
// an OpenGL context must be created and set as current
// define the surface properties
var info = new SKImageInfo(256, 256);
// create the surface
var context = GRContext.CreateGl();
var surface = SKSurface.Create(context, false, info);
// get the canvas from the surface
var canvas = surface.Canvas;
// draw on the canvas ...
建構 PDF 文件
PDF 後端使用 SKDocumentSKSurface,因為文件必須包含多個頁面。
// create the document
var stream = SKFileWStream.OpenStream("document.pdf");
var document = SKDocument.CreatePdf(stream);
// get the canvas from the page
var canvas = document.BeginPage(256, 256);
// draw on the canvas ...
// end the page and document
document.EndPage();
document.Close();
建構 XPS 文件 (實驗性)
XPS 後端使用 SKDocumentSKSurface,因為文件必須包含多個頁面。
// create the document
var stream = SKFileWStream.OpenStream("document.xps");
var document = SKDocument.CreateXps(stream);
// get the canvas from the page
var canvas = document.BeginPage(256, 256);
// draw on the canvas ...
// end the page and document
document.EndPage();
document.Close();
構建 SVG 畫布 (實驗性)
SVG 後端使用 SKSvgCanvas.
// create the canvas
var stream = SKFileWStream.OpenStream("image.svg");
var writer = new SKXmlStreamWriter(stream);
var canvas = SKSvgCanvas.Create(SKRect.Create(256, 256), writer);
// draw on the canvas ...
構建畫面
XPS 後端使用 SKPictureRecorderSKSurface。
// create the picture recorder
var recorder = new SKPictureRecorder();
// get the canvas from the page
var canvas = recorder.BeginRecording(SKRect.Create(256, 256));
// draw on the canvas ...
// finish recording
var picture = recorder.EndRecording();
構建一個虛無畫布 (用於測試)
空畫布是一個忽略所有繪圖指令且不做任何動作的畫布。
// create the dummy canvas
var canvas = new SKNoDrawCanvas(256, 256);
// draw on the canvas ...
Transformations
畫布支援多種 2D 變換。 與其他 2D 圖形系統如 CoreGraphics 或 Cairo 不同,SKCanvas 將轉換擴展至包含透視功能。
你可以使用 Scale、 Skew、 Translate、 RotateDegrees來 RotateRadians 執行一些最常見的二維變換。
為了更精確的控制,你可以用 和 SetMatrix 來設定任意轉換SKMatrixConcat,將轉換串接SKMatrix到目前使用的矩陣。
可用來 ResetMatrix 重置矩陣的狀態。
繪圖
繪圖操作可以設定 SKPaint 參數來影響繪圖。 你用 SKPaint 物件來快取樣式和顏色資訊,來繪製幾何體、文字和點陣圖。
剪裁與狀態
你可以透過呼叫 Save 保持電流轉換矩陣的方法來保存電流變換,然後你可以透過 Restore or RestoreToCount 方法修改矩陣並恢復先前狀態。
此外,也可以推送一個新狀態 SaveLayer ,將區域的螢幕外複製,繪圖完成後,呼叫 Restore() 將螢幕外點陣圖複製到畫布上的方法。
建構函式
| 名稱 | Description |
|---|---|
| SKCanvas(SKBitmap) |
建立一個畫布,裡面有指定的點陣圖來繪製。 |
屬性
| 名稱 | Description |
|---|---|
| Context |
取得與此畫布相關的 GPU 錄影上下文。 |
| DeviceClipBounds |
取得目前剪輯的邊界(以裝置座標表示)。 |
| Handle |
取得或設定 handle 為底層的原生物件。 (繼承來源 SKObject) |
| IgnorePublicDispose |
獲得一個值,表示該呼叫 Dispose() 是否應該 no-op。 (繼承來源 SKNativeObject) |
| IsClipEmpty |
會得到一個值,表示目前的片段是否是空的。 |
| IsClipRect |
會得到一個值,表示目前的剪輯是否是長方形。 |
| IsDisposed |
會獲得一個值,表示該物件是否已被處置。 (繼承來源 SKNativeObject) |
| LocalClipBounds |
取得當前剪輯的邊界(以當地座標計算)。 |
| OwnsHandle |
取得或設定一個值,指示該物件是否擁有其句柄,並在處理時應銷毀原生物件。 (繼承來源 SKNativeObject) |
| SaveCount |
取得畫布私有堆疊上的矩陣/剪輯狀態數量。 |
| Surface |
取得與此畫布相關的表面。 |
| TotalMatrix |
取得畫布上目前的矩陣。 |
| TotalMatrix44 |
取得畫布上目前的 4x4 變換矩陣。 |