fix typo
[mcs.git] / class / System.Drawing / System.Drawing / ImageConverter.cs
blobb3f961b1ad05f2a68dae73495adea40bdd5a9e26
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 sourceType)
53 if (sourceType == typeof (System.Byte []))
54 return true;
55 else
56 return false;
59 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
61 if ((destinationType == typeof (System.Byte [])) || (destinationType == typeof (System.String)))
62 return true;
63 else
64 return false;
67 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
69 byte [] bytes = value as byte [];
70 if (bytes == null)
71 return base.ConvertFrom (context, culture, value);
73 MemoryStream ms = new MemoryStream (bytes);
75 return Image.FromStream (ms);
78 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
80 if (value == null)
81 return "(none)";
83 if (value is System.Drawing.Image) {
84 if (destinationType == typeof (string)) {
85 return value.ToString ();
86 } else if (CanConvertTo (null, destinationType)) {
87 //came here means destinationType is byte array ;
88 MemoryStream ms = new MemoryStream ();
89 ((Image)value).Save (ms, ((Image)value).RawFormat);
90 return ms.GetBuffer ();
94 string msg = Locale.GetText ("ImageConverter can not convert from type '{0}'.", value.GetType ());
95 throw new NotSupportedException (msg);
98 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
100 return TypeDescriptor.GetProperties (typeof (Image), attributes);
103 public override bool GetPropertiesSupported (ITypeDescriptorContext context )
105 return true;