ImageList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Revision: 1.1 $
  27. // $Modtime: $
  28. // $Log: ImageList.cs,v $
  29. // Revision 1.1 2004/07/15 20:05:28 pbartok
  30. // - Implemented ImageList and ImageList.ImageCollection classes
  31. // - Added ColorDepth enumeration
  32. // - Updated SWF VS.Net project
  33. //
  34. //
  35. // COMPLETE
  36. using System.Collections;
  37. using System.ComponentModel;
  38. using System.Drawing;
  39. using System.Drawing.Imaging;
  40. namespace System.Windows.Forms {
  41. public sealed class ImageList : System.ComponentModel.Component {
  42. #region Local Variables
  43. private ColorDepth color_depth;
  44. private ImageCollection image_collection;
  45. private Size size;
  46. private Color transparency_color;
  47. private Delegate handler;
  48. private ImageListStreamer image_stream;
  49. #endregion // Local Variables
  50. #region Sub-classes
  51. public sealed class ImageCollection : IList, ICollection, IEnumerable {
  52. #region ImageCollection Local Variables
  53. private ImageList owner;
  54. private ArrayList list;
  55. #endregion // ImageCollection Local Variables
  56. #region ImageCollection Private Constructors
  57. internal ImageCollection(ImageList owner) {
  58. this.owner=owner;
  59. }
  60. #endregion // ImageCollection Private Constructor
  61. #region ImageCollection Public Instance Properties
  62. public int Count {
  63. get {
  64. return list.Count;
  65. }
  66. }
  67. public bool Empty {
  68. get {
  69. return list.Count==0;
  70. }
  71. }
  72. public bool IsReadOnly {
  73. get {
  74. return list.IsReadOnly;
  75. }
  76. }
  77. public Image this[int index] {
  78. get {
  79. if (index<0 || index>=list.Count) {
  80. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  81. }
  82. return (Image)list[index];
  83. }
  84. set {
  85. if (index<0 || index>=list.Count) {
  86. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  87. }
  88. if (value==null) {
  89. throw new ArgumentOutOfRangeException("value", value, "Image cannot be null");
  90. }
  91. list[index]=value;
  92. // What happens if the bitmap had a previous 'MakeTransparent' done to it?
  93. ((Bitmap)list[index]).MakeTransparent(owner.transparency_color);
  94. }
  95. }
  96. #endregion // ImageCollection Public Instance Properties
  97. #region ImageCollection Private Instance Methods
  98. private int AddInternal(Image image) {
  99. int width;
  100. int height;
  101. PixelFormat format;
  102. width=owner.ImageSize.Width;
  103. height=owner.ImageSize.Height;
  104. switch(owner.color_depth) {
  105. case ColorDepth.Depth4Bit: format=PixelFormat.Format4bppIndexed; break;
  106. case ColorDepth.Depth8Bit: format=PixelFormat.Format8bppIndexed; break;
  107. case ColorDepth.Depth16Bit: format=PixelFormat.Format16bppRgb555; break;
  108. case ColorDepth.Depth24Bit: format=PixelFormat.Format24bppRgb; break;
  109. case ColorDepth.Depth32Bit: format=PixelFormat.Format32bppRgb; break;
  110. default: format=PixelFormat.Format32bppRgb; break;
  111. }
  112. // Check if we can add straight or if we have to resize
  113. if (image.Width!=width || image.Height!=height || image.PixelFormat!=format) {
  114. Graphics g;
  115. Bitmap reformatted_image;
  116. reformatted_image = new Bitmap(width, height, format);
  117. g=Graphics.FromImage(reformatted_image);
  118. g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
  119. g.Dispose();
  120. return list.Add(reformatted_image);
  121. } else {
  122. return list.Add(image);
  123. }
  124. }
  125. internal void Dispose() {
  126. if (list!=null) {
  127. for (int i=0; i<list.Count; i++) {
  128. ((Image)list[i]).Dispose();
  129. }
  130. }
  131. }
  132. #endregion // ImageCollection Private Instance Methods
  133. #region ImageCollection Public Instance Methods
  134. public int Add(Image value, Color transparentColor) {
  135. if (value==null) {
  136. throw new ArgumentNullException("value", "Cannot add null image");
  137. }
  138. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  139. return AddInternal(value);
  140. }
  141. public void Add(Icon value) {
  142. Image image;
  143. image = value.ToBitmap();
  144. if (value==null || image==null) {
  145. throw new ArgumentNullException("value", "Cannot add null icon");
  146. }
  147. ((Bitmap)image).MakeTransparent(owner.transparency_color);
  148. AddInternal(image);
  149. }
  150. public void Add(Image value) {
  151. if (value==null) {
  152. throw new ArgumentNullException("value", "Cannot add null image");
  153. }
  154. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  155. AddInternal(value);
  156. }
  157. public int AddStrip(Image value) {
  158. int image_count;
  159. int width;
  160. int height;
  161. Bitmap image;
  162. Graphics g;
  163. if (value==null) {
  164. throw new ArgumentNullException("value", "Cannot add null images");
  165. }
  166. if ((value.Width % owner.ImageSize.Width) != 0) {
  167. throw new ArgumentException("Strip is not a multiple of the ImageList with", "value");
  168. }
  169. // MSDN: The number of images is inferred from the width. A strip is multiple images side-by-side
  170. width=owner.ImageSize.Width;
  171. height=owner.ImageSize.Height;
  172. image_count=value.Width/width;
  173. for (int i=0; i<image_count; i++) {
  174. image = new Bitmap(value, width, height);
  175. g = Graphics.FromImage(image);
  176. g.DrawImage(value, new Rectangle(0, 0, width, height), i*width, 0, width, height, GraphicsUnit.Pixel);
  177. AddInternal(image);
  178. g.Dispose();
  179. image.Dispose();
  180. }
  181. // FIXME - is this right? MSDN says to return the index, but we might have multiple...
  182. return image_count;
  183. }
  184. public void Clear() {
  185. list.Clear();
  186. }
  187. public bool Contains(Image image) {
  188. return list.Contains(image);
  189. }
  190. public IEnumerator GetEnumerator() {
  191. return list.GetEnumerator();
  192. }
  193. public int IndexOf(Image image) {
  194. return list.IndexOf(image);
  195. }
  196. public void Remove(Image image) {
  197. list.Remove(image);
  198. }
  199. public void RemoveAt(int index) {
  200. if (index<0 || index>=list.Count) {
  201. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  202. }
  203. list.RemoveAt(index);
  204. }
  205. #endregion // ImageCollection Public Instance Methods
  206. #region ImageCollection Interface Properties
  207. object IList.this[int index] {
  208. get {
  209. if (index<0 || index>=list.Count) {
  210. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  211. }
  212. return this[index];
  213. }
  214. set {
  215. if (!(value is Bitmap)) {
  216. throw new ArgumentException("Object of type Image required", "value");
  217. }
  218. this[index]=(Image)value;
  219. }
  220. }
  221. bool IList.IsFixedSize {
  222. get {
  223. return false;
  224. }
  225. }
  226. bool IList.IsReadOnly {
  227. get {
  228. return list.IsReadOnly;
  229. }
  230. }
  231. bool ICollection.IsSynchronized {
  232. get {
  233. return list.IsSynchronized;
  234. }
  235. }
  236. object ICollection.SyncRoot {
  237. get {
  238. return list.SyncRoot;
  239. }
  240. }
  241. #endregion // ImageCollection Interface Properties
  242. #region ImageCollection Interface Methods
  243. int IList.Add(object value) {
  244. if (value == null) {
  245. throw new ArgumentNullException("value", "Cannot add null images");
  246. }
  247. if (!(value is Bitmap)) {
  248. throw new ArgumentException("Object of type Image required", "value");
  249. }
  250. return list.Add(value);
  251. }
  252. bool IList.Contains(object value) {
  253. if (!(value is Bitmap)) {
  254. throw new ArgumentException("Object of type Image required", "value");
  255. }
  256. return this.Contains((Image) value);
  257. }
  258. int IList.IndexOf(object value) {
  259. if (!(value is Bitmap)) {
  260. throw new ArgumentException("Object of type Image required", "value");
  261. }
  262. return this.IndexOf((Image) value);
  263. }
  264. void IList.Insert(int index, object value) {
  265. if (!(value is Bitmap)) {
  266. throw new ArgumentException("Object of type Image required", "value");
  267. }
  268. list.Insert(index, value);
  269. }
  270. void IList.Remove(object value) {
  271. if (!(value is Bitmap)) {
  272. throw new ArgumentException("Object of type Image required", "value");
  273. }
  274. list.Remove(value);
  275. }
  276. void ICollection.CopyTo(Array array, int index) {
  277. if (list.Count>0) {
  278. list.CopyTo(array, index);
  279. }
  280. }
  281. #endregion // ImageCollection Interface Methods
  282. }
  283. #endregion // Sub-classes
  284. #region Public Constructors
  285. public ImageList() {
  286. color_depth = ColorDepth.Depth8Bit;
  287. transparency_color = Color.Transparent;
  288. image_collection = new ImageCollection(this);
  289. }
  290. #endregion // Public Constructors
  291. #region Public Instance Properties
  292. public ColorDepth ColorDepth {
  293. get {
  294. return this.color_depth;
  295. }
  296. set {
  297. this.color_depth=value;
  298. }
  299. }
  300. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  301. public IntPtr Handle {
  302. get {
  303. return IntPtr.Zero;
  304. }
  305. }
  306. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  307. public bool HandleCreated {
  308. get {
  309. return false;
  310. }
  311. }
  312. public ImageCollection Images {
  313. get {
  314. return this.image_collection;
  315. }
  316. }
  317. public Size ImageSize {
  318. get {
  319. return this.size;
  320. }
  321. set {
  322. if (value.Width<1 || value.Width>255 || value.Height<1 || value.Height>255) {
  323. throw new ArgumentException("ImageSize width and height must be between 1 and 255", "value");
  324. }
  325. this.size=value;
  326. }
  327. }
  328. public ImageListStreamer ImageStream {
  329. get {
  330. return image_stream;
  331. }
  332. set {
  333. image_stream = value;
  334. }
  335. }
  336. public Color TransparentColor {
  337. get {
  338. return this.transparency_color;
  339. }
  340. set {
  341. this.transparency_color=value;
  342. }
  343. }
  344. #endregion // Public Instance Properties
  345. #region Public Instance Methods
  346. public void Draw(Graphics g, Point pt, int index) {
  347. this.Draw(g, pt.X, pt.Y, this.size.Width, this.size.Height, index);
  348. }
  349. public void Draw(Graphics g, int x, int y, int width, int height, int index) {
  350. }
  351. public override string ToString() {
  352. return "ImageList Size "+this.size.Width.ToString()+"x"+this.size.Height.ToString()+", Depth "+this.color_depth.ToString()+", Transparency color "+this.transparency_color.ToString();
  353. }
  354. #endregion // Public Instance Methods
  355. #region Protected Instance Methods
  356. protected override void Dispose(bool disposing) {
  357. if (image_collection!=null) {
  358. image_collection.Dispose();
  359. }
  360. }
  361. #endregion // Protected Instance Methods
  362. }
  363. }