Picture.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. //
  25. using System;
  26. using System.IO;
  27. using System.Drawing;
  28. namespace System.Windows.Forms.RTF {
  29. internal class Picture {
  30. private Minor image_type;
  31. private Image image;
  32. private MemoryStream data;
  33. private float width = -1;
  34. private float height = -1;
  35. private readonly static float dpix;
  36. static Picture ()
  37. {
  38. dpix = TextRenderer.GetDpi ().Width;
  39. }
  40. public Picture ()
  41. {
  42. }
  43. public Minor ImageType {
  44. get { return image_type; }
  45. set { image_type = value; }
  46. }
  47. public MemoryStream Data {
  48. get {
  49. if (data == null)
  50. data = new MemoryStream ();
  51. return data;
  52. }
  53. }
  54. public float Width {
  55. get {
  56. float w = width;
  57. if (w == -1) {
  58. if (image == null)
  59. image = ToImage ();
  60. w = image.Width;
  61. }
  62. return w;
  63. }
  64. }
  65. public float Height {
  66. get {
  67. float h = height;
  68. if (h == -1) {
  69. if (image == null)
  70. image = ToImage ();
  71. h = image.Height;
  72. }
  73. return h;
  74. }
  75. }
  76. public SizeF Size {
  77. get {
  78. return new SizeF (Width, Height);
  79. }
  80. }
  81. public void SetWidthFromTwips (int twips)
  82. {
  83. width = (int) (((float) twips / 1440.0F) * dpix + 0.5F);
  84. }
  85. public void SetHeightFromTwips (int twips)
  86. {
  87. height = (int) (((float) twips / 1440.0F) * dpix + 0.5F);
  88. }
  89. //
  90. // Makes sure that we got enough information to actually use the image
  91. //
  92. public bool IsValid ()
  93. {
  94. if (data == null)
  95. return false;
  96. switch (image_type) {
  97. case Minor.PngBlip:
  98. case Minor.WinMetafile:
  99. break;
  100. default:
  101. return false;
  102. }
  103. return true;
  104. }
  105. public void DrawImage (Graphics dc, float x, float y, bool selected)
  106. {
  107. if (image == null)
  108. image = ToImage ();
  109. float height = this.height;
  110. float width = this.width;
  111. if (height == -1)
  112. height = image.Height;
  113. if (width == -1)
  114. width = image.Width;
  115. dc.DrawImage (image, x, y, width, height);
  116. }
  117. public Image ToImage ()
  118. {
  119. // Reset the data stream position to the beginning
  120. data.Position = 0;
  121. return Image.FromStream (data);
  122. }
  123. }
  124. }