**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / ImageConverter.cs
blob406ffd8c634af1ee3f28108c38a7503ff23c62ac
1 //
2 // System.Drawing.ImageConverter.cs
3 //
4 // Author:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2002 Ximian, Inc
9 //
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:
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.
34 using System;
35 using System.ComponentModel;
36 using System.Globalization;
37 using System.IO;
38 using System.Drawing.Imaging;
40 namespace System.Drawing
42 /// <summary>
43 /// Summary description for ImageConverter.
44 /// </summary>
45 public class ImageConverter : TypeConverter
47 public ImageConverter ()
51 public override bool CanConvertFrom (ITypeDescriptorContext context, Type srcType)
53 if (srcType == typeof (System.Byte[]))
54 return true;
55 else
56 return false;
59 public override bool CanConvertTo (ITypeDescriptorContext context, Type destType)
61 if ((destType == typeof (System.Byte[])) || (destType == typeof (System.String)))
62 return true;
63 else
64 return false;
67 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object val)
69 byte [] bytes = val as byte [];
70 if (bytes == null)
71 return base.ConvertFrom (context, culture, val);
73 MemoryStream ms = new MemoryStream (bytes);
75 return Image.FromStream (ms);
78 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object val, Type destType )
80 if ((val is System.Drawing.Image) && (destType == typeof (System.String)))
81 return val.ToString ();
82 else if (CanConvertTo (null, destType)){
83 //came here means destType is byte array ;
84 MemoryStream ms = new MemoryStream ();
85 ((Image)val).Save (ms, ((Image)val).RawFormat);
86 return ms.GetBuffer ();
87 }else
88 return new NotSupportedException ("ImageConverter can not convert from " + val.GetType ());
91 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object val, Attribute[] attribs )
93 Type type = typeof (System.Drawing.Image);
94 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (type);
95 PropertyDescriptorCollection returnPDC = pdc;
96 if (attribs !=null) {
97 int length = attribs.Length;
98 foreach (PropertyDescriptor pd in pdc) {
99 bool found = false;
100 AttributeCollection attributes = pd.Attributes;
101 //Apply the filter on the PropertyDescriptor
102 //If any attribute is found from attribs, in
103 //PropertyDescriptor then retain that particular
104 //PropertyDescriptor else discard it.
105 for (int i=0; i<length; i++) {
106 if (attributes.Contains (attribs [i])) {
107 found = true;
108 break;
111 if (!found)
112 returnPDC.Remove (pd);
115 return returnPDC;
118 public override bool GetPropertiesSupported (ITypeDescriptorContext context )
120 return true;