fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.Collections.Concurrent;
  9.  
  10. namespace BitmapImageSpeed
  11. {
  12. public partial class MainWindow : Window
  13. {
  14. public MainWindow()
  15. {
  16. InitializeComponent();
  17.  
  18. const string path1 = @"K:\同人\[ひぐま屋] もしも近所にHなサービスをしてくれる銭湯があったら\0title.jpg";
  19. const string path2 = @"M:\Pictures\RYLSKY_ART\aaliyah-love\178894\00.jpg";
  20. const int count = 10;
  21.  
  22. Debug.Print("Without Scaling");
  23. putDecodeTime(path1, doTest1, count);
  24. putDecodeTime(path2, doTest1, count);
  25.  
  26. //Debug.Print("With Scaling");
  27. //putDecodeTime(path1, doTest2, count);
  28. //putDecodeTime(path2, doTest2, count);
  29.  
  30. image1.Source = new BitmapImage(new Uri(path1));
  31. }
  32.  
  33. private void putDecodeTime(string path, Action<byte[]> act, int count = 10)
  34. {
  35. var data = File.ReadAllBytes(path);
  36.  
  37. var time_ms = getTime(() =>
  38. {
  39. var bag = new List<Task>();
  40. var x = new ConcurrentBag<double>();
  41. for (int i = 0; i < count; ++i)
  42. {
  43. bag.Add(Task.Run(() => x.Add(getTime(() => act(data)))));
  44. //act(data);
  45. }
  46. Task.WaitAll(bag.ToArray());
  47. return;
  48. });
  49.  
  50. Debug.Print($"{time_ms / count} ms");
  51. }
  52.  
  53. private double getTime(Action act)
  54. {
  55. var st = new Stopwatch();
  56. st.Start();
  57.  
  58. act();
  59.  
  60. st.Stop();
  61. return st.Elapsed.Ticks / 10000;
  62. }
  63.  
  64. private void doTest1(byte[] data)
  65. {
  66. using (var ms = new MemoryStream(data))
  67. {
  68. var image = new BitmapImage();
  69. image.BeginInit();
  70. image.StreamSource = ms;
  71. image.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
  72. image.CacheOption = BitmapCacheOption.OnLoad;
  73. image.EndInit();
  74. image.Freeze();
  75. }
  76. }
  77.  
  78. private void doTest2(byte[] data)
  79. {
  80. using (var ms = new MemoryStream(data))
  81. {
  82. var image = new BitmapImage();
  83. image.BeginInit();
  84. image.StreamSource = ms;
  85. image.CacheOption = BitmapCacheOption.OnLoad;
  86.  
  87. /* !!! */
  88. image.DecodePixelHeight = 128;
  89.  
  90. image.EndInit();
  91. image.Freeze();
  92. }
  93. }
  94. }
  95. }
  96.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,22): error CS0234: The type or namespace name `Media' does not exist in the namespace `System.Windows'. Are you missing an assembly reference?
prog.cs(12,39): error CS0246: The type or namespace name `Window' could not be found. Are you missing an assembly reference?
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty