public static BitmapSource StringToBitmapSource(this string str, int fontSize, System.Windows.Media.Color foreground, System.Windows.Media.Color background) { TextBlock tbX = new TextBlock(); tbX.FontFamily = new System.Windows.Media.FontFamily("Consolas"); tbX.Foreground = new System.Windows.Media.SolidColorBrush(foreground); tbX.Background = new System.Windows.Media.SolidColorBrush(background); tbX.TextAlignment = TextAlignment.Center; tbX.FontSize = fontSize; tbX.FontStretch = FontStretches.Normal; tbX.FontWeight = FontWeights.Medium; tbX.Text = str; var size = tbX.MeasureString(); tbX.Width = size.Width; tbX.Height = size.Height; tbX.Measure(new Size(size.Width, size.Height)); tbX.Arrange(new Rect(new Size(size.Width, size.Height))); return tbX.ToBitmapSource(); }