SxdCliMainForm.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Windows.Forms;
  6. namespace ScreenExtenderClient
  7. {
  8. public partial class SxdCliMainForm : Form
  9. {
  10. private Image _image;
  11. private Exception _lastException;
  12. public SxdCliMainForm()
  13. {
  14. InitializeComponent();
  15. if (Program.Args.Count > 1) SxdClVwPanel.Text = Program.Args[1];
  16. }
  17. private void UpdateTimer_Tick(object sender, EventArgs e)
  18. {
  19. var old = _image;
  20. try
  21. {
  22. var cl = new HttpClient();
  23. var httpResponseMessage = cl.GetAsync(Program.Args[0]).Result;
  24. var bytes = httpResponseMessage.Content.ReadAsByteArrayAsync().Result;
  25. var ms = new MemoryStream(bytes);
  26. _image = Image.FromStream(ms);
  27. _lastException = null;
  28. }
  29. catch (Exception exception)
  30. {
  31. _lastException = exception;
  32. Console.WriteLine(exception);
  33. _image = null;
  34. }
  35. old?.Dispose();
  36. SxdClVwPanel.Invalidate();
  37. }
  38. private void SxdClVwPanel_Paint(object sender, PaintEventArgs e)
  39. {
  40. e.Graphics.Clear(BackColor);
  41. if (_image != null) e.Graphics.DrawImage(_image, Point.Empty);
  42. if (_lastException != null) ControlPaint.DrawStringDisabled(e.Graphics, _lastException.ToString(), Font, ForeColor, new Rectangle(0, 0, SxdClVwPanel.Width, SxdClVwPanel.Height), new StringFormat());
  43. }
  44. }
  45. }