| | 1 | | using System.Globalization; |
| | 2 | | using System.Windows; |
| | 3 | | using System.Windows.Controls; |
| | 4 | | using System.Windows.Media; |
| | 5 | | using System.Windows.Media.Imaging; |
| | 6 | |
|
| | 7 | | namespace SampleAppPlus |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Interaction logic for CustomControl.xaml |
| | 11 | | /// </summary> |
| | 12 | | public partial class CustomControl : UserControl |
| | 13 | | { |
| 2 | 14 | | public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string |
| | 15 | |
|
| 2 | 16 | | public CustomControl() |
| 2 | 17 | | { |
| 2 | 18 | | InitializeComponent(); |
| 2 | 19 | | } |
| | 20 | |
|
| | 21 | | public string Message |
| | 22 | | { |
| 39 | 23 | | get { return (string)GetValue(MessageProperty); } |
| 24 | 24 | | set { SetValue(MessageProperty, value); } |
| | 25 | | } |
| | 26 | |
|
| | 27 | | protected override void OnRender(DrawingContext drawingContext) |
| 2 | 28 | | { |
| 4 | 29 | | myImage.Source = WriteText(Message); ; |
| 2 | 30 | | } |
| | 31 | |
|
| | 32 | | private static void OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 7 | 33 | | { |
| 7 | 34 | | var current = d as CustomControl; |
| 7 | 35 | | current.myImage.Source = WriteText((string)e.NewValue); |
| 7 | 36 | | } |
| | 37 | |
|
| | 38 | | private static RenderTargetBitmap WriteText(string text) |
| 9 | 39 | | { |
| 9 | 40 | | DrawingVisual drawingVisual = new DrawingVisual(); |
| 9 | 41 | | using (DrawingContext drawingContext = drawingVisual.RenderOpen()) |
| 9 | 42 | | { |
| 9 | 43 | | FormattedText ft = new FormattedText(text ?? string.Empty, CultureInfo.GetCultureInfo("en-us"), FlowDire |
| 9 | 44 | | drawingContext.DrawText(ft, new Point(0, 0)); |
| 9 | 45 | | } |
| 9 | 46 | | RenderTargetBitmap bmp = new RenderTargetBitmap(510, 50, 96, 96, PixelFormats.Pbgra32); |
| 9 | 47 | | bmp.Render(drawingVisual); |
| 9 | 48 | | return bmp; |
| 9 | 49 | | } |
| | 50 | | } |
| | 51 | | } |