PropertyItem2.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. namespace SmartCoalApplication.SystemLayer
  10. {
  11. [Serializable]
  12. internal sealed class PropertyItem2
  13. {
  14. private const string piElementName = "exif";
  15. private const string idPropertyName = "id";
  16. private const string lenPropertyName = "len";
  17. private const string typePropertyName = "type";
  18. private const string valuePropertyName = "value";
  19. private int id;
  20. private int len;
  21. private short type;
  22. private byte[] value;
  23. public int Id
  24. {
  25. get
  26. {
  27. return id;
  28. }
  29. }
  30. public int Len
  31. {
  32. get
  33. {
  34. return len;
  35. }
  36. }
  37. public short Type
  38. {
  39. get
  40. {
  41. return type;
  42. }
  43. }
  44. public byte[] Value
  45. {
  46. get
  47. {
  48. return (byte[])value.Clone();
  49. }
  50. }
  51. public PropertyItem2(int id, int len, short type, byte[] value)
  52. {
  53. this.id = id;
  54. this.len = len;
  55. this.type = type;
  56. if (value == null)
  57. {
  58. this.value = new byte[0];
  59. }
  60. else
  61. {
  62. this.value = (byte[])value.Clone();
  63. }
  64. if (len != this.value.Length)
  65. {
  66. }
  67. }
  68. public string ToBlob()
  69. {
  70. string blob = string.Format("<{0} {1}=\"{2}\" {3}=\"{4}\" {5}=\"{6}\" {7}=\"{8}\" />",
  71. piElementName,
  72. idPropertyName, this.id.ToString(CultureInfo.InvariantCulture),
  73. lenPropertyName, this.len.ToString(CultureInfo.InvariantCulture),
  74. typePropertyName, this.type.ToString(CultureInfo.InvariantCulture),
  75. valuePropertyName, Convert.ToBase64String(this.value));
  76. return blob;
  77. }
  78. [MethodImpl(MethodImplOptions.Synchronized)]
  79. public PropertyItem ToPropertyItem()
  80. {
  81. PropertyItem pi = GetPropertyItem();
  82. pi.Id = this.Id;
  83. pi.Len = this.Len;
  84. pi.Type = this.Type;
  85. pi.Value = this.Value;
  86. return pi;
  87. }
  88. public static PropertyItem2 FromPropertyItem(PropertyItem pi)
  89. {
  90. return new PropertyItem2(pi.Id, pi.Len, pi.Type, pi.Value);
  91. }
  92. private static string GetProperty(string blob, string propertyName)
  93. {
  94. string findMe = propertyName + "=\"";
  95. int startIndex = blob.IndexOf(findMe, StringComparison.OrdinalIgnoreCase) + findMe.Length;
  96. int endIndex = blob.IndexOf("\"", startIndex, StringComparison.OrdinalIgnoreCase);
  97. string propertyValue = blob.Substring(startIndex, endIndex - startIndex);
  98. return propertyValue;
  99. }
  100. public static PropertyItem2 FromBlob(string blob)
  101. {
  102. PropertyItem2 pi2;
  103. if (blob.Length > 0 && blob[0] == '<')
  104. {
  105. string idStr = GetProperty(blob, idPropertyName);
  106. string lenStr = GetProperty(blob, lenPropertyName);
  107. string typeStr = GetProperty(blob, typePropertyName);
  108. string valueStr = GetProperty(blob, valuePropertyName);
  109. int id = int.Parse(idStr, CultureInfo.InvariantCulture);
  110. int len = int.Parse(lenStr, CultureInfo.InvariantCulture);
  111. short type = short.Parse(typeStr, CultureInfo.InvariantCulture);
  112. byte[] value = Convert.FromBase64String(valueStr);
  113. pi2 = new PropertyItem2(id, len, type, value);
  114. }
  115. else
  116. {
  117. byte[] bytes = Convert.FromBase64String(blob);
  118. MemoryStream ms = new MemoryStream(bytes);
  119. BinaryFormatter bf = new BinaryFormatter();
  120. SerializationFallbackBinder sfb = new SerializationFallbackBinder();
  121. sfb.AddAssembly(Assembly.GetExecutingAssembly());
  122. bf.Binder = sfb;
  123. pi2 = (PropertyItem2)bf.Deserialize(ms);
  124. }
  125. return pi2;
  126. }
  127. private static Image propertyItemImage;
  128. [MethodImpl(MethodImplOptions.Synchronized)]
  129. private static PropertyItem GetPropertyItem()
  130. {
  131. if (propertyItemImage == null)
  132. {
  133. Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PaintDotNet.SystemLayer.PropertyItem.png");
  134. propertyItemImage = Image.FromStream(stream);
  135. }
  136. PropertyItem pi = propertyItemImage.PropertyItems[0];
  137. pi.Id = 0;
  138. pi.Len = 0;
  139. pi.Type = 0;
  140. pi.Value = new byte[0];
  141. return pi;
  142. }
  143. }
  144. }