次の方法で共有


方法 : ビジュアルにテキストを描画する

DrawingContext オブジェクトを使用して DrawingVisual にテキストを描画する方法を次の例に示します。 描画コンテキストは、DrawingVisual オブジェクトの RenderOpen メソッドを呼び出すことによって返されます。 描画コンテキストにグラフィックスとテキストを描画することができます。

描画コンテキストにテキストを描画するには、DrawingContext オブジェクトの DrawText メソッドを使用します。 描画コンテキストへのコンテンツの描画が完了したら、Close メソッドを呼び出して描画コンテキストを閉じ、コンテンツを保持します。

使用例

        ' 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
// 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;
}
メモメモ

前のコード例を含むコード サンプル全体については、DrawingVisual を使用したヒット テストのサンプルを参照してください。