Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. FlowDocument flowDoc = new FlowDocument();
  2. using (MemoryStream mem = new MemoryStream(bytesFromDB)
  3. {
  4. TextRange textRange = new TextRange(
  5. flowDoc.ContentStart,
  6. flowDoc.ContentEnd);
  7. textRange.Load(mem, DataFormats.XamlPackage);
  8. }
  9.  
  10. textRange.Load(mem, DataFormats.XamlPackage);
  11.  
  12. var sw = Stopwatch.StartNew();
  13. var doc = new FlowDocument();
  14. var tr = new TextRange(doc.ContentStart, doc.ContentEnd);
  15. using (var stream = File.OpenRead(@"D:CSharp Language Specification.rtf"))
  16. tr.Load(stream, DataFormats.Rtf);
  17. Viewer.Document = doc;
  18. sw.Stop();
  19. Status.Text = "Finished in " + sw.Elapsed;
  20.  
  21. try
  22. {
  23. var sw = Stopwatch.StartNew();
  24. Viewer.Document = null;
  25. Status.Text = "Loading...";
  26. Progress<string> p = new Progress<string>(s => Status.Text = s);
  27. var xaml = await Task.Run(
  28. () => LoadAsXamlInBackground(@"D:CSharp Language Specification.rtf", p));
  29. Status.Text = "Rendering...";
  30. var doc = (FlowDocument)XamlReader.Load(xaml);
  31. Viewer.Document = doc;
  32. sw.Stop();
  33. Status.Text = "Finished in " + sw.Elapsed;
  34. }
  35. catch (Exception ex)
  36. {
  37. Status.Text = "Failed: " + ex.ToString();
  38. }
  39.  
  40. // ...
  41. MemoryStream LoadAsXamlInBackground(string filename, IProgress<string> progress)
  42. {
  43. var ms = new MemoryStream();
  44. var doc = new FlowDocument();
  45. var tr = new TextRange(doc.ContentStart, doc.ContentEnd);
  46. using (var stream = File.OpenRead(filename))
  47. tr.Load(stream, DataFormats.Rtf);
  48. progress.Report("Loaded, converting...");
  49. XamlWriter.Save(doc, ms);
  50. progress.Report("Converted...");
  51. ms.Position = 0;
  52. return ms;
  53. }
  54.  
  55. Main
  56.  
  57. Thread th = new Thread ( delegate () { load (); } );
  58. th.TrySetApartmentState ( ApartmentState.STA );
  59. th.Start ();
Add Comment
Please, Sign In to add comment