make fallbacks for GdiCharSet, GdiVerticalFont
[mono.git] / mcs / class / System.Drawing / System.Drawing / ImageConverter.cs
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 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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.
32 //
33
34 using System;
35 using System.ComponentModel;
36 using System.Globalization;
37 using System.IO;
38 using System.Drawing.Imaging;
39
40 namespace System.Drawing
41 {
42         /// <summary>
43         /// Summary description for ImageConverter.
44         /// </summary>
45         public class ImageConverter : TypeConverter
46         {               
47                 public ImageConverter ()
48                 {
49                 }
50                 
51                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type srcType)
52                 {
53                         if (srcType == typeof (System.Byte[]))
54                                 return true;
55                         else
56                                 return false; 
57                 }
58
59                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destType)
60                 {
61                         if ((destType == typeof (System.Byte[])) || (destType == typeof (System.String)))
62                                 return true;
63                         else
64                                 return false;
65                 }
66                 
67                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object val)
68                 {
69                         byte [] bytes = val as byte [];
70                         if (bytes == null)
71                                 return base.ConvertFrom (context, culture, val);
72                         
73                         MemoryStream ms = new MemoryStream (bytes);
74                         
75                         return Image.FromStream (ms);   
76                 }
77
78                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object val, Type destType )
79                 {
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 ());
89                 }
90
91                 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object val, Attribute[] attribs)
92                 {
93                         return TypeDescriptor.GetProperties (typeof (Image), attribs);
94                 }
95
96                 public override bool GetPropertiesSupported (ITypeDescriptorContext context )
97                 {
98                         return true; 
99                 }
100         }
101 }