Icon.jvm.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Drawing.Icon.cs
  3. //
  4. // Authors:
  5. // Andrew Skiba ([email protected])
  6. // Dennis Hayes ([email protected])
  7. // Andreas Nahr ([email protected])
  8. // Sanjay Gupta ([email protected])
  9. //
  10. // Copyright (C) 2005 Mainsoft, Corp. http://mainsoft.com
  11. // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
  12. // Copyright (C) 2004 Novell, Inc. http://www.novell.com
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.IO;
  35. using System.Drawing.Imaging;
  36. using System.Runtime.Serialization;
  37. using System.Runtime.InteropServices;
  38. using System.ComponentModel;
  39. namespace System.Drawing
  40. {
  41. [Serializable]
  42. [ComVisible (false)]
  43. [TypeConverter(typeof(IconConverter))]
  44. public sealed class Icon
  45. : MarshalByRefObject, ISerializable, ICloneable, IDisposable
  46. {
  47. private System.Drawing.Bitmap _bitmap;
  48. #region Ctors
  49. private void SelectSize (int width, int height) {
  50. int count = _bitmap.GetFrameCount (FrameDimension.Resolution);
  51. bool sizeObtained = false;
  52. for (int i=0; i<count; i++){
  53. _bitmap.SelectActiveFrame (
  54. System.Drawing.Imaging.FrameDimension.Resolution, i);
  55. if (!sizeObtained)
  56. if (_bitmap.Height==height && _bitmap.Width==width) {
  57. sizeObtained = true;
  58. break;
  59. }
  60. }
  61. if (!sizeObtained){
  62. uint largestSize = 0;
  63. Bitmap tmpBmp = _bitmap;
  64. for (int j=0; j<count; j++){
  65. tmpBmp.SelectActiveFrame (FrameDimension.Resolution, j);
  66. uint thisSize = (uint)_bitmap.Height * (uint)_bitmap.Width;
  67. if (thisSize >= largestSize){
  68. largestSize = thisSize;
  69. _bitmap = tmpBmp;
  70. }
  71. }
  72. }
  73. }
  74. private Icon () {
  75. }
  76. internal Icon (Bitmap bitmap) {
  77. _bitmap = bitmap;
  78. }
  79. public Icon (Icon original, int width, int height) {
  80. _bitmap = original._bitmap;
  81. SelectSize (width, height);
  82. }
  83. public Icon (Icon original, Size size)
  84. :this (original, size.Width, size.Height) {
  85. }
  86. public Icon (Stream stream)
  87. : this (stream, 32, 32) {
  88. }
  89. public Icon (Stream stream, int width, int height)
  90. {
  91. _bitmap = new Bitmap (stream, false, ImageFormat.Icon);
  92. SelectSize (width, height);
  93. }
  94. public Icon (string fileName) {
  95. _bitmap = new Bitmap (fileName, false, ImageFormat.Icon);
  96. }
  97. public Icon (Type type, string resource)
  98. {
  99. using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
  100. if (s == null)
  101. throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
  102. _bitmap = new Bitmap (s, false, ImageFormat.Icon);
  103. }
  104. }
  105. [MonoTODO]
  106. private Icon (SerializationInfo info, StreamingContext context)
  107. {
  108. //FIXME, need to check how MS stores Icon structure
  109. //Will serialized form help
  110. throw new NotImplementedException ();
  111. }
  112. #endregion
  113. [MonoTODO]
  114. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. public void Dispose ()
  119. {
  120. #if INTPTR_SUPPORT
  121. if (winHandle!=IntPtr.Zero)
  122. winHandle = IntPtr.Zero;
  123. #endif
  124. }
  125. public object Clone ()
  126. {
  127. Icon newIcon = new Icon ();
  128. newIcon._bitmap = (Bitmap)_bitmap.Clone ();
  129. return newIcon;
  130. }
  131. #if INTPTR_SUPPORT
  132. [MonoTODO]
  133. public static Icon FromHandle (IntPtr handle)
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. #endif
  138. public void Save (Stream outputStream) {
  139. _bitmap.Save (outputStream, System.Drawing.Imaging.ImageFormat.Icon);
  140. }
  141. public Bitmap ToBitmap () {
  142. return _bitmap;
  143. }
  144. public override string ToString ()
  145. {
  146. //is this correct, this is what returned by .Net
  147. return "<Icon>";
  148. }
  149. #if INTPTR_SUPPORT
  150. [Browsable (false)]
  151. public IntPtr Handle {
  152. get {
  153. return winHandle;
  154. }
  155. }
  156. #endif
  157. [Browsable (false)]
  158. public int Height {
  159. get {
  160. return _bitmap.Height;
  161. }
  162. }
  163. public Size Size {
  164. get {
  165. return _bitmap.Size;
  166. }
  167. }
  168. [Browsable (false)]
  169. public int Width {
  170. get {
  171. return _bitmap.Width;
  172. }
  173. }
  174. }
  175. }