CustomImageControl.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace PDF_Office.CustomControl
  16. {
  17. internal class CustomImageControl : FrameworkElement
  18. {
  19. private DrawingVisual PresentChild { get; set; }
  20. private DrawingContext DrawDC { get; set; }
  21. public CustomImageControl()
  22. {
  23. PresentChild = new DrawingVisual();
  24. AddVisualChild(PresentChild);
  25. AddLogicalChild(PresentChild);
  26. }
  27. protected override Visual GetVisualChild(int index)
  28. {
  29. if (PresentChild == null)
  30. {
  31. throw new ArgumentOutOfRangeException();
  32. }
  33. return PresentChild;
  34. }
  35. protected override int VisualChildrenCount
  36. {
  37. get
  38. {
  39. if (PresentChild != null)
  40. {
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. }
  46. public DrawingContext OpenDraw()
  47. {
  48. if (DrawDC == null)
  49. {
  50. DrawDC = PresentChild?.RenderOpen();
  51. }
  52. return DrawDC;
  53. }
  54. public void CloseDraw()
  55. {
  56. DrawDC?.Close();
  57. DrawDC = null;
  58. }
  59. }
  60. }