MainWindow.xaml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  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 Captcha
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. private Bitmap bitmap = new Bitmap(100, 100);
  25. private string cd = string.Empty;
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. cd = "553Fa";
  30. SetBtm(100, 100);
  31. }
  32. private void SetBtm(int w, int h)
  33. {
  34. var random = new Random();
  35. var pixels = new byte[256 * 256 * 4];
  36. random.NextBytes(pixels);
  37. BitmapSource bitmapSource = BitmapSource.Create(256, 256, 96, 96, PixelFormats.Bgra32, null, pixels, 256 * 4);
  38. var visual = new DrawingVisual();
  39. using (DrawingContext drawingContext = visual.RenderOpen())
  40. {
  41. drawingContext.DrawText(
  42. new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
  43. new Typeface("Segoe UI"), 132, System.Windows.Media.Brushes.Black), new System.Windows.Point(0, 0));
  44. drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256));
  45. }
  46. var image = new DrawingImage(visual.Drawing);
  47. grf.Source = image;
  48. }
  49. }
  50. }