2 // System.Drawing.ImageConverter.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;
38 using System.Drawing.Imaging;
40 namespace System.Drawing
43 /// Summary description for ImageConverter.
45 public class ImageConverter : TypeConverter
47 public ImageConverter ()
51 public override bool CanConvertFrom (ITypeDescriptorContext context, Type srcType)
53 if (srcType == typeof (System.Byte[]))
59 public override bool CanConvertTo (ITypeDescriptorContext context, Type destType)
61 if ((destType == typeof (System.Byte[])) || (destType == typeof (System.String)))
67 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object val)
69 byte [] bytes = val as byte [];
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 ();
88 return new NotSupportedException ("ImageConverter can not convert from " + val.GetType ());
91 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object val, Attribute[] attribs)
93 return TypeDescriptor.GetProperties (typeof (Image), attribs);
96 public override bool GetPropertiesSupported (ITypeDescriptorContext context )