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 sourceType
)
53 if (sourceType
== typeof (System
.Byte
[]))
59 public override bool CanConvertTo (ITypeDescriptorContext context
, Type destinationType
)
61 if ((destinationType
== typeof (System
.Byte
[])) || (destinationType
== typeof (System
.String
)))
67 public override object ConvertFrom (ITypeDescriptorContext context
, CultureInfo culture
, object value)
69 byte [] bytes
= value as byte [];
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
)
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
)