CaptchaPage.xaml.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace ПодготовкаКДемо1.Pages
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для CaptchaPage.xaml
  21. /// </summary>
  22. public partial class CaptchaPage : Page
  23. {
  24. public CaptchaPage()
  25. {
  26. InitializeComponent();
  27. timer();
  28. }
  29. private async void timer()
  30. {
  31. await Task.Run(() => captcha());
  32. }
  33. //ImageSource image;
  34. private void captcha()
  35. {
  36. Random random = new Random();
  37. int num = random.Next(1000, 9999);
  38. var pixels = new byte[150 * 150 * 4];
  39. random.NextBytes(pixels);
  40. var visual = new DrawingVisual();
  41. BitmapSource bitmapSource = BitmapSource.Create(150, 150, 96, 96, PixelFormats.Pbgra32, null, pixels, 150 * 4);
  42. using (DrawingContext drawingContext = visual.RenderOpen())
  43. {
  44. drawingContext.DrawText(
  45. new FormattedText(num.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 100, System.Windows.Media.Brushes.Red),
  46. new System.Windows.Point(0, 150 / 2));
  47. drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256));
  48. }
  49. var image = new DrawingImage(visual.Drawing);
  50. Application.Current.Dispatcher.Invoke(() => { CaptchaImage.Source = image; });
  51. }
  52. }
  53. }