1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.IO;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.Text;
|
---|
7 |
|
---|
8 | namespace SharpVectors.Runtime
|
---|
9 | {
|
---|
10 | public sealed class EmbeddedBitmapDataConverter : TypeConverter
|
---|
11 | {
|
---|
12 | #region Constructors and Destructor
|
---|
13 |
|
---|
14 | public EmbeddedBitmapDataConverter()
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 | #endregion
|
---|
19 |
|
---|
20 | #region Public Methods
|
---|
21 |
|
---|
22 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
---|
23 | {
|
---|
24 | return sourceType == typeof( string );
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
---|
29 | {
|
---|
30 | if( destinationType == typeof( string ) )
|
---|
31 | {
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 | return base.CanConvertTo( context, destinationType );
|
---|
35 | }
|
---|
36 |
|
---|
37 | public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
|
---|
38 | {
|
---|
39 | byte[] toDecode = Convert.FromBase64String((string)value);
|
---|
40 |
|
---|
41 | MemoryStream memoryStream = new MemoryStream(toDecode);
|
---|
42 | return new EmbeddedBitmapData(memoryStream);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
|
---|
46 | {
|
---|
47 | if( destinationType == typeof(string))
|
---|
48 | {
|
---|
49 | EmbeddedBitmapData bitmapInfo = (EmbeddedBitmapData)value;
|
---|
50 | MemoryStream memoryStream = bitmapInfo.Stream;
|
---|
51 |
|
---|
52 | return Convert.ToBase64String(memoryStream.ToArray());
|
---|
53 | }
|
---|
54 |
|
---|
55 | return base.ConvertTo( context, culture, value, destinationType );
|
---|
56 | }
|
---|
57 |
|
---|
58 | #endregion Methods
|
---|
59 | }
|
---|
60 | }
|
---|