Surrogates.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:9k
源码类别:

GIS编程

开发平台:

C#

  1. // Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
  2. //
  3. // This file is part of SharpMap.
  4. // SharpMap is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. // 
  9. // SharpMap is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU Lesser General Public License for more details.
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with SharpMap; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using System.Runtime.Serialization;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. namespace SharpMap.Utilities
  23. {
  24. /// <summary>
  25. /// Helper class for serializing System.Drawing.Pen and System.Drawing.Brush
  26. /// </summary>
  27. public class Surrogates
  28. {
  29. /// <summary>
  30. /// Gets the surrogate selecteds for System.Drawing.Pen and System.Drawing.Brush
  31. /// </summary>
  32. /// <returns>SurrogateSelector</returns>
  33. public static SurrogateSelector GetSurrogateSelectors()
  34. {
  35. System.Runtime.Serialization.SurrogateSelector ss = new System.Runtime.Serialization.SurrogateSelector();
  36. ss.AddSurrogate(typeof(Pen), new StreamingContext(StreamingContextStates.All), new SharpMap.Utilities.Surrogates.PenSurrogate());
  37. ss.AddSurrogate(typeof(SolidBrush), new StreamingContext(StreamingContextStates.All), new SharpMap.Utilities.Surrogates.SolidBrushSurrogate());
  38. ss.AddSurrogate(typeof(System.Drawing.TextureBrush), new StreamingContext(StreamingContextStates.All), new SharpMap.Utilities.Surrogates.TextureBrushSurrogate());
  39. ss.AddSurrogate(typeof(Matrix), new StreamingContext(StreamingContextStates.All), new SharpMap.Utilities.Surrogates.MatrixSurrogate());
  40. return ss;
  41. }
  42. /// <summary>
  43. /// Surrogate class used for serializing System.Drawing.SolidBrush
  44. /// </summary>
  45. public class SolidBrushSurrogate : ISerializationSurrogate
  46. {
  47. /// <summary>
  48. /// Populates the provided SerializationInfo with the data needed to serialize the object.
  49. /// </summary>
  50. /// <param name="obj">The object to serialize.</param>
  51. /// <param name="info">The SerializationInfo to populate with data.</param>
  52. /// <param name="context">The destination for this serialization.</param>
  53. public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
  54. {
  55. System.Drawing.SolidBrush brush = (System.Drawing.SolidBrush)obj;
  56. info.AddValue("Color", brush.Color);
  57. }
  58. /// <summary>
  59. /// Populates the object using the information in the SerializationInfo
  60. /// </summary>
  61. /// <param name="obj">The object to populate.</param>
  62. /// <param name="info">The information to populate the object.</param>
  63. /// <param name="context">The source from which the object is deserialized.</param>
  64. /// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param>
  65. /// <returns></returns>
  66. public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  67. {
  68. SolidBrush brush = new SolidBrush((Color)info.GetValue("Color", typeof(Color)));
  69. return null;
  70. }
  71. }
  72. /// <summary>
  73. /// Surrogate class used for serializing System.Drawing.TextureBrush
  74. /// </summary>
  75. public class TextureBrushSurrogate : ISerializationSurrogate
  76. {
  77. /// <summary>
  78. /// Populates the provided SerializationInfo with the data needed to serialize the object.
  79. /// </summary>
  80. /// <param name="obj">The object to serialize.</param>
  81. /// <param name="info">The SerializationInfo to populate with data.</param>
  82. /// <param name="context">The destination for this serialization.</param>
  83. public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
  84. {
  85. TextureBrush brush = (TextureBrush)obj;
  86. info.AddValue("Image",brush.Image);
  87. info.AddValue("Transform", brush.Transform);
  88. info.AddValue("WrapMode", brush.WrapMode);
  89. }
  90. /// <summary>
  91. /// Populates the object using the information in the SerializationInfo
  92. /// </summary>
  93. /// <param name="obj">The object to populate.</param>
  94. /// <param name="info">The information to populate the object.</param>
  95. /// <param name="context">The source from which the object is deserialized.</param>
  96. /// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param>
  97. /// <returns></returns>
  98. public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  99. {
  100. TextureBrush brush = new TextureBrush((Image)info.GetValue("Image", typeof(Image)));
  101. brush.Transform = (Matrix)info.GetValue("Transform", typeof(Matrix));
  102. brush.WrapMode = (WrapMode)info.GetValue("WrapMode", typeof(WrapMode));
  103. return null;
  104. }
  105. }
  106. /// <summary>
  107. /// Surrogate class used for serializing System.Drawing.Pen
  108. /// </summary>
  109. public class PenSurrogate : ISerializationSurrogate
  110. {
  111. /// <summary>
  112. /// Populates the provided SerializationInfo with the data needed to serialize the object.
  113. /// </summary>
  114. /// <param name="obj">The object to serialize.</param>
  115. /// <param name="info">The SerializationInfo to populate with data.</param>
  116. /// <param name="context">The destination for this serialization.</param>
  117. public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
  118. {
  119. System.Drawing.Pen pen = (System.Drawing.Pen)obj;
  120. if (pen.Color != Color.Empty)
  121. {
  122. info.AddValue("Color", pen.Color);
  123. info.AddValue("Width", pen.Width);
  124. info.AddValue("Alignment", pen.Alignment);
  125. //info.AddValue("Brush", pen.Brush);
  126. info.AddValue("CompoundArray", pen.CompoundArray);
  127. //Todo: 
  128. //info.AddValue("CustomEndCap", pen.CustomEndCap);
  129. //info.AddValue("CustomStartCap", pen.CustomStartCap);
  130. //pen.DashCap;
  131. //pen.DashOffset;
  132. info.AddValue("DashPattern", pen.DashPattern);
  133. //pen.DashStyle;
  134. //pen.EndCap;
  135. //pen.LineJoin;
  136. //pen.MiterLimit;
  137. //pen.PenType;
  138. //pen.StartCap;
  139. info.AddValue("Transform", pen.Transform);
  140. }
  141. }
  142. /// <summary>
  143. /// Populates the object using the information in the SerializationInfo
  144. /// </summary>
  145. /// <param name="obj">The object to populate.</param>
  146. /// <param name="info">The information to populate the object.</param>
  147. /// <param name="context">The source from which the object is deserialized.</param>
  148. /// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param>
  149. /// <returns></returns>
  150. public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  151. {
  152. Pen pen = new Pen((Color)info.GetValue("Color", typeof(Color)));
  153. pen.Width = (float)info.GetValue("Width", typeof(float));
  154. pen.Alignment = (PenAlignment)info.GetValue("Alignment", typeof(PenAlignment));
  155. //pen.Brush = (Brush)info.GetValue("Brush", typeof(Brush));
  156. try { pen.CompoundArray = (float[])info.GetValue("CompoundArray", typeof(float[])); } catch { }
  157. //pen.CustomEndCap = (CustomLineCap)info.GetValue("CustomEndCap", typeof(CustomLineCap));
  158. //pen.CustomStartCap = (CustomLineCap)info.GetValue("CustomStartCap", typeof(CustomLineCap));
  159. pen.DashPattern = (float[])info.GetValue("DashPattern", typeof(float[]));
  160. try { pen.Transform = (Matrix)info.GetValue("Transform", typeof(Matrix)); } catch { }
  161. return null;
  162. }
  163. }
  164. /// <summary>
  165. /// Surrogate class used for serializing System.Drawing.Drawing2D.Matrix
  166. /// </summary>
  167. public class MatrixSurrogate : ISerializationSurrogate
  168. {
  169. /// <summary>
  170. /// Populates the provided SerializationInfo with the data needed to serialize the object.
  171. /// </summary>
  172. /// <param name="obj">The object to serialize.</param>
  173. /// <param name="info">The SerializationInfo to populate with data.</param>
  174. /// <param name="context">The destination for this serialization.</param>
  175. public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
  176. {
  177. Matrix mat = (Matrix)obj;
  178. info.AddValue("Elements", mat.Elements);
  179. }
  180. /// <summary>
  181. /// Populates the object using the information in the SerializationInfo
  182. /// </summary>
  183. /// <param name="obj">The object to populate.</param>
  184. /// <param name="info">The information to populate the object.</param>
  185. /// <param name="context">The source from which the object is deserialized.</param>
  186. /// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param>
  187. /// <returns></returns>
  188. public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  189. {
  190. float[] elements = (float[])info.GetValue("Elements", typeof(float[]));
  191. Matrix mat = new Matrix(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]);
  192. return null;
  193. }
  194. }
  195. }
  196. }