2 // System.Drawing.ImageFormatConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Sanjay Gupta (gsanjay@novell.com)
8 // (C) 2002 Ximian, Inc
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.
35 using System.ComponentModel;
36 using System.Globalization;
37 using System.Drawing.Imaging;
38 using System.ComponentModel.Design.Serialization;
39 using System.Reflection;
41 namespace System.Drawing
44 /// Summary description for ImageFormatConverter.
46 public class ImageFormatConverter : TypeConverter
48 public ImageFormatConverter ()
52 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
54 if (sourceType == typeof (string))
57 return base.CanConvertFrom (context, sourceType);
60 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
62 if (destinationType == typeof (string))
65 if (destinationType == typeof (InstanceDescriptor))
68 return base.CanConvertTo (context, destinationType);
71 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
73 // we must be able to convert from short names and long names
74 string strFormat = (value as string);
75 if (strFormat == null) {
76 // case #1, this is not a string
77 return base.ConvertFrom (context, culture, value);
78 } else if (strFormat [0] == '[') {
79 // case #2, this is probably a long format (guid)
80 if (strFormat.Equals (ImageFormat.Bmp.ToString ()))
81 return ImageFormat.Bmp;
82 else if (strFormat.Equals (ImageFormat.Emf.ToString ()))
83 return ImageFormat.Emf;
84 else if (strFormat.Equals (ImageFormat.Exif.ToString ()))
85 return ImageFormat.Exif;
86 else if (strFormat.Equals (ImageFormat.Gif.ToString ()))
87 return ImageFormat.Gif;
88 else if (strFormat.Equals (ImageFormat.Icon.ToString ()))
89 return ImageFormat.Icon;
90 else if (strFormat.Equals (ImageFormat.Jpeg.ToString ()))
91 return ImageFormat.Jpeg;
92 else if (strFormat.Equals (ImageFormat.MemoryBmp.ToString ()))
93 return ImageFormat.MemoryBmp;
94 else if (strFormat.Equals (ImageFormat.Png.ToString ()))
95 return ImageFormat.Png;
96 else if (strFormat.Equals (ImageFormat.Tiff.ToString ()))
97 return ImageFormat.Tiff;
98 else if (strFormat.Equals (ImageFormat.Wmf.ToString ()))
99 return ImageFormat.Wmf;
101 // case #3, this is probably a short format
102 if (String.Compare (strFormat, "Bmp", true, CultureInfo.InvariantCulture) == 0)
103 return ImageFormat.Bmp;
104 else if (String.Compare (strFormat, "Emf", true, CultureInfo.InvariantCulture) == 0)
105 return ImageFormat.Emf;
106 else if (String.Compare (strFormat, "Exif", true, CultureInfo.InvariantCulture) == 0)
107 return ImageFormat.Exif;
108 else if (String.Compare (strFormat, "Gif", true, CultureInfo.InvariantCulture) == 0)
109 return ImageFormat.Gif;
110 else if (String.Compare (strFormat, "Icon", true, CultureInfo.InvariantCulture) == 0)
111 return ImageFormat.Icon;
112 else if (String.Compare (strFormat, "Jpeg", true, CultureInfo.InvariantCulture) == 0)
113 return ImageFormat.Jpeg;
114 else if (String.Compare (strFormat, "MemoryBmp", true, CultureInfo.InvariantCulture) == 0)
115 return ImageFormat.MemoryBmp;
116 else if (String.Compare (strFormat, "Png", true, CultureInfo.InvariantCulture) == 0)
117 return ImageFormat.Png;
118 else if (String.Compare (strFormat, "Tiff", true, CultureInfo.InvariantCulture) == 0)
119 return ImageFormat.Tiff;
120 else if (String.Compare (strFormat, "Wmf", true, CultureInfo.InvariantCulture) == 0)
121 return ImageFormat.Wmf;
123 // last case, this is an unknown string
124 return base.ConvertFrom (context, culture, value);
127 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
129 if (value is ImageFormat) {
130 ImageFormat c = (ImageFormat) value;
132 if (c.Guid.Equals (ImageFormat.Bmp.Guid))
134 else if (c.Guid.Equals (ImageFormat.Emf.Guid))
136 else if (c.Guid.Equals (ImageFormat.Exif.Guid))
138 else if (c.Guid.Equals (ImageFormat.Gif.Guid))
140 else if (c.Guid.Equals (ImageFormat.Icon.Guid))
142 else if (c.Guid.Equals (ImageFormat.Jpeg.Guid))
144 else if (c.Guid.Equals (ImageFormat.MemoryBmp.Guid))
146 else if (c.Guid.Equals (ImageFormat.Png.Guid))
148 else if (c.Guid.Equals (ImageFormat.Tiff.Guid))
150 else if (c.Guid.Equals (ImageFormat.Wmf.Guid))
153 if (destinationType == typeof (string)) {
154 return prop != null ? prop : c.ToString ();
155 } else if (destinationType == typeof (InstanceDescriptor)) {
157 return new InstanceDescriptor (typeof (ImageFormat).GetTypeInfo ().GetProperty (prop), null);
159 ConstructorInfo ctor = typeof(ImageFormat).GetTypeInfo ().GetConstructor (new Type[] {typeof(Guid)} );
160 return new InstanceDescriptor (ctor, new object[] {c.Guid});
165 return base.ConvertTo (context, culture, value, destinationType);
168 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
170 ImageFormat[] list = new ImageFormat [10];
171 list [0] = ImageFormat.MemoryBmp;
172 list [1] = ImageFormat.Bmp;
173 list [2] = ImageFormat.Emf;
174 list [3] = ImageFormat.Wmf;
175 list [4] = ImageFormat.Gif;
176 list [5] = ImageFormat.Jpeg;
177 list [6] = ImageFormat.Png;
178 list [7] = ImageFormat.Tiff;
179 list [8] = ImageFormat.Exif;
180 list [9] = ImageFormat.Icon;
181 return new TypeConverter.StandardValuesCollection (list);
184 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)