0

前winform:</p>

Graphics g = Graphics.FromImage(newimg);
String str = "hello world";
Font font = new Font("Arial", 30);
SolidBrush sbrush = new SolidBrush(Color.Black);
g.DrawString(str, font, sbrush, new PointF(100, 120));

在 WPF 上如何对 InteropBitmap 做同样的事情?

4

1 回答 1

0

Wpf 中的图形对象与 Winforms 不同,因为 Wpf 使用矢量图形。在 Wpf 中,您将使用 DrawingVisual DrawingContext FormattedTextBitmapImage DrawingContext 等效于 Winforms 中的 Graphics 对象。这是一个简单的例子来说明我的意思。

public MainWindow()
{
    InitializeComponent();

    Grid myGrid = new Grid();
    BitmapImage bmp = new BitmapImage(new Uri(@"C:\temp\test.jpg")); //Use the path to your Image 
    DrawingVisual dv = new DrawingVisual();
    DrawingContext dc = dv.RenderOpen();
    dc.DrawImage(bmp, new Rect(100, 100, 300, 300));

    dc.DrawText(new FormattedText("Hello World",
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface("Arial"),
                30, System.Windows.Media.Brushes.Black),
                new System.Windows.Point(100, 120));

    dc.Close();

    myGrid.Background = new VisualBrush(dv); 
    this.Content = myGrid;
}
于 2013-01-08T07:59:35.070 回答