MyBitmap.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using Imazen.WebP;
  10. using PCL.My;
  11. namespace PCL
  12. {
  13. // Token: 0x020001F0 RID: 496
  14. public class MyBitmap
  15. {
  16. // Token: 0x06001760 RID: 5984 RVA: 0x000976CC File Offset: 0x000958CC
  17. public static implicit operator MyBitmap(Image Image)
  18. {
  19. MyBitmap result;
  20. if (Image == null)
  21. {
  22. result = null;
  23. }
  24. else
  25. {
  26. result = new MyBitmap(Image);
  27. }
  28. return result;
  29. }
  30. // Token: 0x06001761 RID: 5985 RVA: 0x000976E8 File Offset: 0x000958E8
  31. public static implicit operator Image(MyBitmap Image)
  32. {
  33. Image result;
  34. if (Image == null)
  35. {
  36. result = null;
  37. }
  38. else
  39. {
  40. result = Image._ContainerIterator;
  41. }
  42. return result;
  43. }
  44. // Token: 0x06001762 RID: 5986 RVA: 0x00097704 File Offset: 0x00095904
  45. public static implicit operator MyBitmap(ImageSource Image)
  46. {
  47. MyBitmap result;
  48. if (Image == null)
  49. {
  50. result = null;
  51. }
  52. else
  53. {
  54. result = new MyBitmap(Image);
  55. }
  56. return result;
  57. }
  58. // Token: 0x06001763 RID: 5987 RVA: 0x00097720 File Offset: 0x00095920
  59. public static implicit operator ImageSource(MyBitmap Image)
  60. {
  61. ImageSource result;
  62. if (Image == null)
  63. {
  64. result = null;
  65. }
  66. else
  67. {
  68. Bitmap containerIterator = Image._ContainerIterator;
  69. Rectangle rect = new Rectangle(0, 0, containerIterator.Width, containerIterator.Height);
  70. BitmapData bitmapData = containerIterator.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  71. try
  72. {
  73. int bufferSize = checked(rect.Width * rect.Height * 4);
  74. result = BitmapSource.Create(containerIterator.Width, containerIterator.Height, (double)containerIterator.HorizontalResolution, (double)containerIterator.VerticalResolution, PixelFormats.Bgra32, null, bitmapData.Scan0, bufferSize, bitmapData.Stride);
  75. }
  76. finally
  77. {
  78. containerIterator.UnlockBits(bitmapData);
  79. }
  80. }
  81. return result;
  82. }
  83. // Token: 0x06001764 RID: 5988 RVA: 0x000977C0 File Offset: 0x000959C0
  84. public static implicit operator MyBitmap(Bitmap Image)
  85. {
  86. MyBitmap result;
  87. if (Image == null)
  88. {
  89. result = null;
  90. }
  91. else
  92. {
  93. result = new MyBitmap(Image);
  94. }
  95. return result;
  96. }
  97. // Token: 0x06001765 RID: 5989 RVA: 0x000977DC File Offset: 0x000959DC
  98. public static implicit operator Bitmap(MyBitmap Image)
  99. {
  100. Bitmap result;
  101. if (Image == null)
  102. {
  103. result = null;
  104. }
  105. else
  106. {
  107. result = Image._ContainerIterator;
  108. }
  109. return result;
  110. }
  111. // Token: 0x06001766 RID: 5990 RVA: 0x000977F8 File Offset: 0x000959F8
  112. public static implicit operator MyBitmap(ImageBrush Image)
  113. {
  114. MyBitmap result;
  115. if (Image == null)
  116. {
  117. result = null;
  118. }
  119. else
  120. {
  121. result = new MyBitmap(Image);
  122. }
  123. return result;
  124. }
  125. // Token: 0x06001767 RID: 5991 RVA: 0x00097814 File Offset: 0x00095A14
  126. public static implicit operator ImageBrush(MyBitmap Image)
  127. {
  128. ImageBrush result;
  129. if (Image == null)
  130. {
  131. result = null;
  132. }
  133. else
  134. {
  135. result = new ImageBrush(new MyBitmap(Image._ContainerIterator));
  136. }
  137. return result;
  138. }
  139. // Token: 0x06001768 RID: 5992 RVA: 0x00002411 File Offset: 0x00000611
  140. public MyBitmap()
  141. {
  142. }
  143. // Token: 0x06001769 RID: 5993 RVA: 0x00097840 File Offset: 0x00095A40
  144. public MyBitmap(string FilePathOrResourceName)
  145. {
  146. try
  147. {
  148. FilePathOrResourceName = FilePathOrResourceName.Replace("pack://application:,,,/images/", ModBase.m_SerializerRepository);
  149. if (FilePathOrResourceName.StartsWithF(ModBase.m_SerializerRepository, false))
  150. {
  151. if (MyBitmap.m_InterceptorIterator.ContainsKey(FilePathOrResourceName))
  152. {
  153. this._ContainerIterator = MyBitmap.m_InterceptorIterator[FilePathOrResourceName]._ContainerIterator;
  154. }
  155. else
  156. {
  157. this._ContainerIterator = new MyBitmap((ImageSource)new ImageSourceConverter().ConvertFromString(FilePathOrResourceName));
  158. MyBitmap.m_InterceptorIterator.TryAdd(FilePathOrResourceName, this._ContainerIterator);
  159. }
  160. }
  161. else
  162. {
  163. using (FileStream fileStream = new FileStream(FilePathOrResourceName, FileMode.Open))
  164. {
  165. byte[] array = new byte[2];
  166. fileStream.Read(array, 0, 2);
  167. fileStream.Seek(0L, SeekOrigin.Begin);
  168. if (array[0] == 82 && array[1] == 73)
  169. {
  170. byte[] array2 = new byte[checked((int)(fileStream.Length - 1L) + 1)];
  171. fileStream.Read(array2, 0, array2.Length);
  172. this._ContainerIterator = MyBitmap.WebPDecoder.DecodeFromBytes(array2);
  173. }
  174. else
  175. {
  176. this._ContainerIterator = new Bitmap(fileStream);
  177. }
  178. }
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. this._ContainerIterator = (Bitmap)MyWpfExtension.RunParser().TryFindResource(FilePathOrResourceName);
  184. if (this._ContainerIterator == null)
  185. {
  186. this._ContainerIterator = new Bitmap(1, 1);
  187. throw new Exception(string.Format("加载 MyBitmap 失败({0})", FilePathOrResourceName), ex);
  188. }
  189. ModBase.Log(ex, string.Format("指定类型有误的 MyBitmap 加载({0})", FilePathOrResourceName), ModBase.LogLevel.Developer, "出现错误");
  190. }
  191. }
  192. // Token: 0x0600176A RID: 5994 RVA: 0x000979F4 File Offset: 0x00095BF4
  193. public MyBitmap(ImageSource Image)
  194. {
  195. using (MemoryStream memoryStream = new MemoryStream())
  196. {
  197. new PngBitmapEncoder
  198. {
  199. Frames =
  200. {
  201. BitmapFrame.Create((BitmapSource)Image)
  202. }
  203. }.Save(memoryStream);
  204. this._ContainerIterator = new Bitmap(memoryStream);
  205. }
  206. }
  207. // Token: 0x0600176B RID: 5995 RVA: 0x0000D147 File Offset: 0x0000B347
  208. public MyBitmap(Image Image)
  209. {
  210. this._ContainerIterator = (Bitmap)Image;
  211. }
  212. // Token: 0x0600176C RID: 5996 RVA: 0x0000D15C File Offset: 0x0000B35C
  213. public MyBitmap(Bitmap Image)
  214. {
  215. this._ContainerIterator = Image;
  216. }
  217. // Token: 0x0600176D RID: 5997 RVA: 0x00097A58 File Offset: 0x00095C58
  218. public MyBitmap(ImageBrush Image)
  219. {
  220. using (MemoryStream memoryStream = new MemoryStream())
  221. {
  222. new BmpBitmapEncoder
  223. {
  224. Frames =
  225. {
  226. BitmapFrame.Create((BitmapSource)Image.ImageSource)
  227. }
  228. }.Save(memoryStream);
  229. this._ContainerIterator = new Bitmap(memoryStream);
  230. }
  231. }
  232. // Token: 0x0600176E RID: 5998 RVA: 0x00097AC0 File Offset: 0x00095CC0
  233. public MyBitmap Clip(int X, int Y, int Width, int Height)
  234. {
  235. Bitmap bitmap = new Bitmap(Width, Height, this._ContainerIterator.PixelFormat);
  236. bitmap.SetResolution(this._ContainerIterator.HorizontalResolution, this._ContainerIterator.VerticalResolution);
  237. checked
  238. {
  239. using (Graphics graphics = Graphics.FromImage(bitmap))
  240. {
  241. graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
  242. graphics.TranslateTransform((float)(0 - X), (float)(0 - Y));
  243. graphics.DrawImage(this._ContainerIterator, new Rectangle(0, 0, this._ContainerIterator.Width, this._ContainerIterator.Height));
  244. }
  245. return bitmap;
  246. }
  247. }
  248. // Token: 0x0600176F RID: 5999 RVA: 0x0000D16C File Offset: 0x0000B36C
  249. public MyBitmap RotateFlip(RotateFlipType Type)
  250. {
  251. Bitmap bitmap = new Bitmap(this._ContainerIterator);
  252. bitmap.SetResolution(this._ContainerIterator.HorizontalResolution, this._ContainerIterator.VerticalResolution);
  253. bitmap.RotateFlip(Type);
  254. return bitmap;
  255. }
  256. // Token: 0x06001770 RID: 6000 RVA: 0x00097B64 File Offset: 0x00095D64
  257. public void Save(string FilePath)
  258. {
  259. BitmapEncoder bitmapEncoder = new PngBitmapEncoder();
  260. bitmapEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)this));
  261. using (FileStream fileStream = new FileStream(FilePath, FileMode.Create))
  262. {
  263. bitmapEncoder.Save(fileStream);
  264. }
  265. }
  266. // Token: 0x04000BD9 RID: 3033
  267. public static ConcurrentDictionary<string, MyBitmap> m_InterceptorIterator = new ConcurrentDictionary<string, MyBitmap>();
  268. // Token: 0x04000BDA RID: 3034
  269. public Bitmap _ContainerIterator;
  270. // Token: 0x020001F1 RID: 497
  271. private class WebPDecoder
  272. {
  273. // Token: 0x06001772 RID: 6002 RVA: 0x0000D1A1 File Offset: 0x0000B3A1
  274. public static Bitmap DecodeFromBytes(byte[] Bytes)
  275. {
  276. if (ModBase.m_StubRepository)
  277. {
  278. throw new Exception("不支持在 32 位系统下加载 WebP 图片。");
  279. }
  280. return new SimpleDecoder().DecodeFromBytes(Bytes, (long)Bytes.Length);
  281. }
  282. }
  283. }
  284. }