using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ПодготовкаКДемо1.Pages { /// /// Логика взаимодействия для CaptchaPage.xaml /// public partial class CaptchaPage : Page { public CaptchaPage() { InitializeComponent(); timer(); } private async void timer() { await Task.Run(() => captcha()); } //ImageSource image; private void captcha() { Random random = new Random(); int num = random.Next(1000, 9999); var pixels = new byte[150 * 150 * 4]; random.NextBytes(pixels); var visual = new DrawingVisual(); BitmapSource bitmapSource = BitmapSource.Create(150, 150, 96, 96, PixelFormats.Pbgra32, null, pixels, 150 * 4); using (DrawingContext drawingContext = visual.RenderOpen()) { drawingContext.DrawText( new FormattedText(num.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 100, System.Windows.Media.Brushes.Red), new System.Windows.Point(0, 150 / 2)); drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256)); } var image = new DrawingImage(visual.Drawing); Application.Current.Dispatcher.Invoke(() => { CaptchaImage.Source = image; }); } } }