PictureBoxComponent.razor 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. @using System.IO
  2. @using System.Drawing
  3. @inherits DialogBaseComponent<PictureBoxControl>
  4. <div style="@GetStyle"></div>
  5. @code {
  6. protected override string GetStyle
  7. => $"{GetControlPosition()} {GetControlAlign()} {GetPictureBoxURL(Control.Image)} padding:0;margin:0;";
  8. protected string GetPictureBoxURL(Image image)
  9. {
  10. #if WASM
  11. // TODO: add support for Wasm
  12. return "";
  13. #else
  14. int hash = image.GetHashCode();
  15. if (!WebReport.PictureCache.ContainsKey(hash.ToString()))
  16. {
  17. using (MemoryStream picStream = new MemoryStream())
  18. {
  19. image.Save(picStream, image.RawFormat);
  20. byte[] imageArray = new byte[picStream.Length];
  21. picStream.Position = 0;
  22. picStream.Read(imageArray, 0, (int)picStream.Length);
  23. WebReport.PictureCache.Add(hash.ToString(), imageArray);
  24. }
  25. }
  26. string url = WebUtils.ToUrl(WebReport.template_ROUTE_BASE_PATH, $"preview.getPicture?");
  27. return $"background: url('{url}reportId={WebReport.ID}&pictureId={hash}') no-repeat !important;-webkit-print-color-adjust:exact;";
  28. #endif
  29. }
  30. }