2013-01-08 59 views
0

前的WinForm:在InteropBitmap如何做同樣的事情在WPF中如何給InteropBitmap繪製字體

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?

+0

Winforms中運行時,它不適合你的工作,例如..?檢查該工作示例 - http://stackoverflow.com/questions/6311545/c-sharp-write-text-on-bitmap – MethodMan

回答

0

圖形在WPF對象比的Winforms不同的,因爲WPF使用矢量圖形。在WPF中,你將使用一個DrawingVisualDrawingContextFormattedTextBitmapImage,在圖形中的WinForms對象的DrawingContext等同。這是展示我的意思的一個快速示例。

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; 
}