共用方式為


如何:繪製文字至視覺效果

下列範例示範如何使用 DrawingContext 物件將文字繪製到 DrawingVisual。 呼叫 DrawingVisual 物件的 RenderOpen 方法會傳回繪圖內容。 您可以將圖形和文字繪製成繪圖內容。

若要將文字繪製到繪圖內容中,請使用 DrawingContext 物件的 DrawText 方法。 當您完成將內容繪製到繪圖內容時,請呼叫 Close 方法來關閉繪圖內容並保存內容。

範例

// Create a DrawingVisual that contains text.
private DrawingVisual CreateDrawingVisualText()
{
    // Create an instance of a DrawingVisual.
    DrawingVisual drawingVisual = new DrawingVisual();

    // Retrieve the DrawingContext from the DrawingVisual.
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    // Draw a formatted text string into the DrawingContext.
    drawingContext.DrawText(
       new FormattedText("Click Me!",
          CultureInfo.GetCultureInfo("en-us"),
          FlowDirection.LeftToRight,
          new Typeface("Verdana"),
          36, System.Windows.Media.Brushes.Black),
          new System.Windows.Point(200, 116));

    // Close the DrawingContext to persist changes to the DrawingVisual.
    drawingContext.Close();

    return drawingVisual;
}
' Create a DrawingVisual that contains text.
Private Function CreateDrawingVisualText() As DrawingVisual
    ' Create an instance of a DrawingVisual.
    Dim drawingVisual As New DrawingVisual()

    ' Retrieve the DrawingContext from the DrawingVisual.
    Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()

    ' Draw a formatted text string into the DrawingContext.
    drawingContext.DrawText(New FormattedText("Click Me!", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Verdana"), 36, Brushes.Black), New Point(200, 116))

    ' Close the DrawingContext to persist changes to the DrawingVisual.
    drawingContext.Close()

    Return drawingVisual
End Function

備註

如需先前程式碼範例出處的完整程式碼範例,請參閱使用 DrawingVisuals 進行點擊測試範例 (英文)