ImageConverter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // System.Drawing.ImageConverter.cs
  3. //
  4. // Author:
  5. // Dennis Hayes ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.IO;
  36. using System.Drawing.Imaging;
  37. namespace System.Drawing
  38. {
  39. /// <summary>
  40. /// Summary description for ImageConverter.
  41. /// </summary>
  42. public class ImageConverter : TypeConverter
  43. {
  44. public ImageConverter ()
  45. {
  46. }
  47. public override bool CanConvertFrom (ITypeDescriptorContext context, Type srcType)
  48. {
  49. if (srcType == typeof (System.Byte[]))
  50. return true;
  51. else
  52. return false;
  53. }
  54. public override bool CanConvertTo (ITypeDescriptorContext context, Type destType)
  55. {
  56. if ((destType == typeof (System.Byte[])) || (destType == typeof (System.String)))
  57. return true;
  58. else
  59. return false;
  60. }
  61. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object val)
  62. {
  63. byte [] bytes = val as byte [];
  64. if (bytes == null)
  65. return base.ConvertFrom (context, culture, val);
  66. MemoryStream ms = new MemoryStream (bytes);
  67. return Image.FromStream (ms);
  68. }
  69. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object val, Type destType)
  70. {
  71. if (val == null)
  72. return "(none)";
  73. if (val is System.Drawing.Image) {
  74. if (destType == typeof (string)) {
  75. return val.ToString ();
  76. } else if (CanConvertTo (null, destType)) {
  77. //came here means destType is byte array ;
  78. MemoryStream ms = new MemoryStream ();
  79. ((Image)val).Save (ms, ((Image)val).RawFormat);
  80. return ms.GetBuffer ();
  81. }
  82. }
  83. string msg = Locale.GetText ("ImageConverter can not convert from type '{0}'.", val.GetType ());
  84. throw new NotSupportedException (msg);
  85. }
  86. public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object val, Attribute[] attribs)
  87. {
  88. return TypeDescriptor.GetProperties (typeof (Image), attribs);
  89. }
  90. public override bool GetPropertiesSupported (ITypeDescriptorContext context )
  91. {
  92. return true;
  93. }
  94. }
  95. }