fork download
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6.  
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. byte[] bytes;
  12. using (var td = new Bitmap(500, 500))
  13. using (var lgb = new LinearGradientBrush(new Point(0, 0), new Point(td.Width, td.Height), Color.Red, Color.Green))
  14. using (var g = Graphics.FromImage(td))
  15. {
  16. g.FillRectangle(lgb, 0, 0, td.Width, td.Height);
  17.  
  18. using (var ms = new MemoryStream())
  19. {
  20. td.Save(ms, ImageFormat.Jpeg);
  21. bytes = ms.ToArray();
  22. }
  23. }
  24.  
  25. Image img;
  26. using (var s = new TestStream(bytes))
  27. {
  28. img = Image.FromStream(s);
  29. Console.WriteLine("Image.FromStream");
  30. }
  31. img.Save("a.jpg");
  32. Console.WriteLine("Image.Save");
  33.  
  34. Console.ReadKey();
  35. }
  36. }
  37.  
  38. internal class TestStream : Stream
  39. {
  40. private readonly byte[] _Data;
  41. private int _Position;
  42.  
  43. internal TestStream(byte[] array)
  44. {
  45. _Data = array;
  46. }
  47.  
  48. public override bool CanRead
  49. {
  50. get
  51. {
  52. return true;
  53. }
  54. }
  55.  
  56. public override bool CanSeek
  57. {
  58. get
  59. {
  60. return true;
  61. }
  62. }
  63.  
  64. public override bool CanWrite
  65. {
  66. get
  67. {
  68. return false;
  69. }
  70. }
  71.  
  72. public override void Flush()
  73. {
  74. }
  75.  
  76. public override long Length
  77. {
  78. get
  79. {
  80. return _Data.Length;
  81. }
  82. }
  83.  
  84. public override long Position
  85. {
  86. get
  87. {
  88. return _Position;
  89. }
  90. set
  91. {
  92. _Position = Math.Min(Math.Max(0, (int)value), _Data.Length);
  93. }
  94. }
  95.  
  96. public override int Read(byte[] buffer, int offset, int count)
  97. {
  98. var l = Math.Min(count, _Data.Length - _Position);
  99. if (l <= 0)
  100. {
  101. return 0;
  102. }
  103.  
  104. Console.WriteLine("Stream.Read {0}, {1}", _Position, l);
  105.  
  106. Array.Copy(_Data, _Position, buffer, offset, l);
  107. _Position += l;
  108. return l;
  109. }
  110.  
  111. public override long Seek(long offset, SeekOrigin origin)
  112. {
  113. switch (origin)
  114. {
  115. case SeekOrigin.Begin:
  116. Position = offset;
  117. break;
  118. case SeekOrigin.Current:
  119. Position += offset;
  120. break;
  121. case SeekOrigin.End:
  122. Position = _Data.Length + offset;
  123. break;
  124. }
  125. return Position;
  126. }
  127.  
  128. public override void SetLength(long value)
  129. {
  130. throw new NotSupportedException();
  131. }
  132.  
  133. public override void Write(byte[] buffer, int offset, int count)
  134. {
  135. throw new NotSupportedException();
  136. }
  137. }
  138.  
  139.  
Runtime error #stdin #stdout 0.11s 47072KB
stdin
Standard input is empty
stdout
Stream.Read 0, 44
Stream.Read 44, 5768
Image.FromStream