You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.7 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. namespace Apewer.Surface
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public class BlockImageViewer : BaseControl
  16. {
  17. private PictureBox _picture = new PictureBox();
  18. private Point _start = new Point(0, 0);
  19. private Point _flow;
  20. private int _padding = 0;
  21. private bool _moving = false;
  22. private bool _inimage = false;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public BlockImageViewer()
  27. {
  28. this.Controls.Add(_picture);
  29. this.Dock = DockStyle.Fill;
  30. this.BackColor = FormsUtility.GraceWall;
  31. _picture.Location = new Point(0, 0);
  32. _picture.Size = Size;
  33. _picture.BackColor = FormsUtility.GraceWall;
  34. Stretch = true;
  35. _flow = new Point(this.Width - _picture.Left, this.Height - _picture.Top);
  36. Adjust();
  37. this.BackColorChanged += Event_This_BackColorChanged;
  38. this.Resize += Event_Main_Resize;
  39. this.MouseEnter += Event_This_MouseEnter;
  40. MouseDown += Event_Picture_Down;
  41. MouseMove += Event_Picture_Move;
  42. MouseUp += Event_Picture_Up;
  43. MouseWheel += Event_Main_Wheel;
  44. _picture.MouseDown += Event_Picture_Down;
  45. _picture.MouseMove += Event_Picture_Move;
  46. _picture.MouseUp += Event_Picture_Up;
  47. _picture.MouseWheel += Event_Picture_Wheel;
  48. _picture.MouseHover += Event_Picture_Hover;
  49. _picture.MouseLeave += Event_Picture_Leave;
  50. }
  51. private void Event_This_MouseEnter(object sender, EventArgs e)
  52. {
  53. if (this.IsHandleCreated) this.Focus();
  54. }
  55. private void Event_This_BackColorChanged(object sender, EventArgs e)
  56. {
  57. _picture.BackColor = this.BackColor;
  58. }
  59. private void Event_Picture_Hover(object sender, EventArgs e)
  60. {
  61. _inimage = true;
  62. }
  63. private void Event_Picture_Leave(object sender, EventArgs e)
  64. {
  65. _inimage = false;
  66. }
  67. private void Event_Main_Wheel(object sender, MouseEventArgs e)
  68. {
  69. if (!_inimage)
  70. {
  71. int vp = BodyPadding;
  72. int vw, vh, vl, vt;
  73. vl = _picture.Width / 2 + _picture.Left;
  74. vt = _picture.Height / 2 + _picture.Top;
  75. if (e.Delta > 0)
  76. {
  77. vw = (int)(_picture.Width * 1.2);
  78. vh = (int)(_picture.Height * 1.2);
  79. if (vw > (this.Width * 10))
  80. {
  81. vw = this.Width * 10;
  82. vh = this.Height * 10;
  83. }
  84. }
  85. else
  86. {
  87. vw = (int)(_picture.Width * 0.8);
  88. vh = (int)(_picture.Height * 0.8);
  89. if (vw < (this.Width * 0.5))
  90. {
  91. vw = (int)(this.Width * 0.5);
  92. vh = (int)(this.Height * 0.5);
  93. }
  94. }
  95. _picture.Size = new Size(vw, vh);
  96. _picture.Left = (int)(vl - _picture.Width / 2);
  97. _picture.Top = (int)(vt - _picture.Height / 2);
  98. }
  99. }
  100. private void Event_Picture_Wheel(object sender, MouseEventArgs e)
  101. {
  102. int vp = BodyPadding;
  103. int vw, vh, vl, vt;
  104. double vx, vy;
  105. vx = (double)e.X / (double)_picture.Width;
  106. vy = (double)e.Y / (double)_picture.Height;
  107. vl = e.X + _picture.Left;
  108. vt = e.Y + _picture.Top;
  109. if (e.Delta > 0)
  110. {
  111. vw = (int)(_picture.Width * 1.2);
  112. vh = (int)(_picture.Height * 1.2);
  113. if (vw > (this.Width * 10))
  114. {
  115. vw = this.Width * 10;
  116. vh = this.Height * 10;
  117. }
  118. }
  119. else
  120. {
  121. vw = (int)(_picture.Width * 0.8);
  122. vh = (int)(_picture.Height * 0.8);
  123. if (vw < (this.Width * 0.5))
  124. {
  125. vw = (int)(this.Width * 0.5);
  126. vh = (int)(this.Height * 0.5);
  127. }
  128. }
  129. _picture.Size = new Size(vw, vh);
  130. _picture.Left = (int)(vl - _picture.Width * vx);
  131. _picture.Top = (int)(vt - _picture.Height * vy);
  132. }
  133. private void Event_Picture_Down(object sender, MouseEventArgs e)
  134. {
  135. switch (e.Button)
  136. {
  137. case MouseButtons.Left:
  138. _start.X = Cursor.Position.X - _picture.Left;
  139. _start.Y = Cursor.Position.Y - _picture.Top;
  140. _moving = true; break;
  141. case MouseButtons.Right:
  142. _moving = false;
  143. Adjust();
  144. break;
  145. }
  146. }
  147. private void Event_Picture_Move(object sender, MouseEventArgs e)
  148. {
  149. if (_moving)
  150. {
  151. _picture.Left = Cursor.Position.X - _start.X;
  152. _picture.Top = Cursor.Position.Y - _start.Y;
  153. }
  154. }
  155. private void Event_Picture_Up(object sender, MouseEventArgs e)
  156. {
  157. switch (e.Button)
  158. {
  159. case MouseButtons.Left:
  160. _moving = false;
  161. break;
  162. }
  163. }
  164. private void Event_Main_Resize(object sender, EventArgs e) { Adjust(); }
  165. /// <summary>
  166. ///
  167. /// </summary>
  168. public void Adjust()
  169. {
  170. int vp = BodyPadding;
  171. _picture.Width = this.Width - vp * 2;
  172. _picture.Height = this.Height - vp * 2;
  173. _picture.Location = new Point(vp, vp);
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. public int BodyPadding
  179. {
  180. get
  181. {
  182. int vw = this.Width / 2 - 2;
  183. int vh = this.Height / 2 - 2;
  184. int vm = (vw > vh) ? vh : vw;
  185. return (_padding > vm) ? vm : _padding;
  186. }
  187. set
  188. {
  189. if (value >= 0) _padding = value;
  190. }
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. public Image GetImage()
  196. {
  197. return _picture.BackgroundImage;
  198. }
  199. /// <summary>
  200. ///
  201. /// </summary>
  202. public void LoadImage(byte[] image)
  203. {
  204. Clear();
  205. if (image.Length < 1) return;
  206. if (!this.IsHandleCreated) return;
  207. if (!_picture.IsHandleCreated) return;
  208. Invoke(new Invoker(delegate ()
  209. {
  210. var memory = new MemoryStream(image);
  211. var bitmap = new Bitmap(memory);
  212. _picture.BackgroundImage = bitmap;
  213. }));
  214. }
  215. /// <summary>
  216. ///
  217. /// </summary>
  218. public void LoadImage(Image image)
  219. {
  220. Clear();
  221. _picture.BackgroundImage = image;
  222. }
  223. /// <summary></summary>
  224. public void LoadImage(string path)
  225. {
  226. if (File.Exists(path))
  227. {
  228. try
  229. {
  230. var vdata = File.ReadAllBytes(path);
  231. LoadImage(vdata);
  232. }
  233. catch { Clear(); }
  234. }
  235. else { Clear(); }
  236. }
  237. /// <summary>
  238. ///
  239. /// </summary>
  240. public void Clear()
  241. {
  242. if (_picture.BackgroundImage != null) _picture.BackgroundImage.Dispose();
  243. _picture.BackgroundImage = null;
  244. }
  245. /// <summary>
  246. ///
  247. /// </summary>
  248. public void Clean()
  249. {
  250. Clear();
  251. }
  252. /// <summary>
  253. ///
  254. /// </summary>
  255. public bool Stretch
  256. {
  257. get
  258. {
  259. return (_picture.BackgroundImageLayout == ImageLayout.Stretch) ? true : false;
  260. }
  261. set
  262. {
  263. if (value) _picture.BackgroundImageLayout = ImageLayout.Stretch;
  264. else _picture.BackgroundImageLayout = ImageLayout.Zoom;
  265. }
  266. }
  267. /// <summary>
  268. ///
  269. /// </summary>
  270. public Size ImageSize
  271. {
  272. get
  273. {
  274. if (_picture.BackgroundImage != null) return _picture.BackgroundImage.Size;
  275. else return new Size(0, 0);
  276. }
  277. }
  278. }
  279. }
  280. #endif