2 // System.Drawing.Icon.cs
5 // Andrew Skiba (andrews@mainsoft.com)
6 // Dennis Hayes (dennish@Raytek.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // Sanjay Gupta (gsanjay@novell.com)
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
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
36 using System.Drawing.Imaging;
37 using System.Runtime.Serialization;
38 using System.Runtime.InteropServices;
39 using System.ComponentModel;
41 namespace System.Drawing
45 [TypeConverter(typeof(IconConverter))]
46 public sealed class Icon
47 : MarshalByRefObject, ISerializable, ICloneable, IDisposable
49 private System.Drawing.Bitmap _bitmap;
52 private void SelectSize (int width, int height) {
53 int count = _bitmap.GetFrameCount (FrameDimension.Resolution);
54 bool sizeObtained = false;
55 for (int i=0; i<count; i++){
56 _bitmap.SelectActiveFrame (
57 System.Drawing.Imaging.FrameDimension.Resolution, i);
59 if (_bitmap.Height==height && _bitmap.Width==width) {
67 Bitmap tmpBmp = _bitmap;
68 for (int j=0; j<count; j++){
69 tmpBmp.SelectActiveFrame (FrameDimension.Resolution, j);
70 uint thisSize = (uint)_bitmap.Height * (uint)_bitmap.Width;
71 if (thisSize >= largestSize){
72 largestSize = thisSize;
82 internal Icon (Bitmap bitmap) {
86 public Icon (Icon original, int width, int height) {
87 _bitmap = original._bitmap;
88 SelectSize (width, height);
91 public Icon (Icon original, Size size)
92 :this (original, size.Width, size.Height) {
95 public Icon (Stream stream)
96 : this (stream, 32, 32) {
99 public Icon (Stream stream, int width, int height)
101 _bitmap = new Bitmap (stream, false, ImageFormat.Icon);
102 SelectSize (width, height);
105 public Icon (string fileName) {
106 _bitmap = new Bitmap (fileName, false, ImageFormat.Icon);
109 public Icon (Type type, string resource)
111 using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
113 throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
114 _bitmap = new Bitmap (s, false, ImageFormat.Icon);
119 private Icon (SerializationInfo info, StreamingContext context)
121 //FIXME, need to check how MS stores Icon structure
122 //Will serialized form help
123 throw new NotImplementedException ();
128 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
130 throw new NotImplementedException ();
133 public void Dispose ()
136 if (winHandle!=IntPtr.Zero)
137 winHandle = IntPtr.Zero;
141 public object Clone ()
143 Icon newIcon = new Icon ();
144 newIcon._bitmap = (Bitmap)_bitmap.Clone ();
150 public static Icon FromHandle (IntPtr handle)
152 throw new NotImplementedException ();
155 public void Save (Stream outputStream) {
156 _bitmap.Save (outputStream, System.Drawing.Imaging.ImageFormat.Icon);
159 public Bitmap ToBitmap () {
163 public override string ToString ()
165 //is this correct, this is what returned by .Net
171 public IntPtr Handle {
181 return _bitmap.Height;
194 return _bitmap.Width;