FormPortraitSelect.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace HslCommunication.BasicFramework
  10. {
  11. /// <summary>
  12. /// 一个正方形图形选择窗口,可以获取指定的分辨率
  13. /// </summary>
  14. public partial class FormPortraitSelect : Form
  15. {
  16. /// <summary>
  17. /// 实例化一个对象
  18. /// </summary>
  19. public FormPortraitSelect()
  20. {
  21. InitializeComponent();
  22. }
  23. /// <summary>
  24. /// 是否有图片存在
  25. /// </summary>
  26. private bool HasPicture { get; set; }
  27. /// <summary>
  28. /// 已选择的图形大小
  29. /// </summary>
  30. private Rectangle RectangleSelected { get; set; }
  31. private Rectangle RectangleMoved { get; set; }
  32. /// <summary>
  33. /// 在控件显示的图片的大小,按照比例缩放以后
  34. /// </summary>
  35. private Rectangle RectangleImage { get; set; }
  36. private void pictureBox1_MouseEnter(object sender, EventArgs e)
  37. {
  38. IsMouseOver = true;
  39. pictureBox1.Refresh();
  40. }
  41. private void pictureBox1_MouseLeave(object sender, EventArgs e)
  42. {
  43. IsMouseOver = false;
  44. pictureBox1.Refresh();
  45. }
  46. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  47. {
  48. if (IsMouseOver && HasPicture)
  49. {
  50. if (RectangleSelected.Contains(e.Location))
  51. {
  52. Cursor = Cursors.SizeAll;
  53. if (IsMouseDown)
  54. {
  55. MoveByPoint(e.Location);
  56. }
  57. }
  58. else
  59. {
  60. Cursor = Cursors.Default;
  61. }
  62. }
  63. }
  64. private void MoveByPoint(Point point)
  65. {
  66. //点击并移动图形
  67. if (RectangleImage.Width >= RectangleImage.Height)
  68. {
  69. //只能左右移动
  70. int offect_x = point.X - MouseMovePrecives.X;
  71. int location_x = offect_x + RectangleSelected.X;
  72. if (location_x >= 0 && location_x <= RectangleImage.Width - RectangleSelected.Width)
  73. {
  74. //移动成功
  75. RectangleSelected = new Rectangle(location_x, RectangleSelected.Y, RectangleSelected.Width, RectangleSelected.Height);
  76. pictureBox1.Refresh();
  77. }
  78. }
  79. else
  80. {
  81. //只能上下移动
  82. int offect_y = point.Y - MouseMovePrecives.Y;
  83. int location_y = offect_y + RectangleSelected.Y;
  84. if (location_y >= 0 && location_y <= RectangleImage.Height - RectangleSelected.Height)
  85. {
  86. //移动成功
  87. RectangleSelected = new Rectangle(RectangleSelected.X, location_y, RectangleSelected.Width, RectangleSelected.Height);
  88. pictureBox1.Refresh();
  89. }
  90. }
  91. MouseMovePrecives = point;
  92. }
  93. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  94. {
  95. if (IsMouseOver && HasPicture)
  96. {
  97. if(RectangleSelected.Contains(e.Location))
  98. {
  99. IsMouseDown = true;
  100. MouseDownPoint = e.Location;
  101. MouseMovePrecives = e.Location;
  102. }
  103. }
  104. pictureBox1.Focus();
  105. }
  106. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  107. {
  108. if (HasPicture)
  109. {
  110. if (RectangleSelected.Contains(e.Location))
  111. {
  112. SetImgaeMiniShow();
  113. }
  114. }
  115. IsMouseDown = false;
  116. pictureBox1.Refresh();
  117. }
  118. private void SetImgaeMiniShow()
  119. {
  120. //区域还原
  121. Rectangle des = RectangleRestore();
  122. pictureBox2.Image?.Dispose();
  123. pictureBox2.Image = GetSpecicalSizeFromImage(100,des);
  124. pictureBox3.Image?.Dispose();
  125. pictureBox3.Image = GetSpecicalSizeFromImage(64, des);
  126. pictureBox4.Image?.Dispose();
  127. pictureBox4.Image = GetSpecicalSizeFromImage(32, des);
  128. }
  129. private Rectangle RectangleRestore()
  130. {
  131. int x = pictureBox1.Image.Width;
  132. int y = pictureBox1.Image.Height;
  133. if (x < y)
  134. {
  135. return new Rectangle(0, RectangleSelected.Y * y / 370, x, x);
  136. }
  137. else
  138. {
  139. return new Rectangle(RectangleSelected.X * x / 370, 0, y, y);
  140. }
  141. }
  142. private Bitmap GetSpecicalSizeFromImage(int size,Rectangle des)
  143. {
  144. Bitmap bitmap = new Bitmap(size, size);
  145. using (Graphics g2 = Graphics.FromImage(bitmap))
  146. {
  147. g2.DrawImage(pictureBox1.Image, new Rectangle(0, 0, size, size), des, GraphicsUnit.Pixel);
  148. }
  149. return bitmap;
  150. }
  151. private void userButton1_Click(object sender, EventArgs e)
  152. {
  153. //文件选择
  154. using (OpenFileDialog open = new OpenFileDialog())
  155. {
  156. open.Multiselect = false;
  157. open.Title = "请选择一张图片";
  158. open.Filter = "图片文件(*.jpg)|*.jpg|图片文件(*.png)|*.png";
  159. if (open.ShowDialog() == DialogResult.OK)
  160. {
  161. //加载文件图片
  162. LoadPictureFile(open.FileName);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 增加一张图片的路径
  168. /// </summary>
  169. /// <param name="picPath"></param>
  170. private void LoadPictureFile(string picPath)
  171. {
  172. Bitmap bitmap = null;
  173. try
  174. {
  175. bitmap = (Bitmap)Image.FromFile(picPath);
  176. int x = bitmap.Width;
  177. int y = bitmap.Height;
  178. if (x > y)
  179. {
  180. y = y * 370 / x;
  181. x = 370 - 1;
  182. }
  183. else
  184. {
  185. x = x * 370 / y;
  186. y = 370 - 1;
  187. }
  188. label1.Text = $"({x},{y})";
  189. RectangleImage = new Rectangle((370 - x) / 2, (370 - y) / 2, x, y);
  190. if (RectangleImage.Width >= RectangleImage.Height)
  191. {
  192. RectangleSelected = new Rectangle(RectangleImage.X, RectangleImage.Y, RectangleImage.Height, RectangleImage.Height);
  193. }
  194. else
  195. {
  196. RectangleSelected = new Rectangle(RectangleImage.X, RectangleImage.Y, RectangleImage.Width, RectangleImage.Width);
  197. }
  198. HasPicture = true;
  199. pictureBox1.Refresh();
  200. }
  201. catch(Exception ex)
  202. {
  203. SoftBasic.ShowExceptionMessage(ex);
  204. return;
  205. }
  206. pictureBox1.Image = bitmap;
  207. SetImgaeMiniShow();
  208. }
  209. private void FormPortraitSelect_Load(object sender, EventArgs e)
  210. {
  211. }
  212. private Brush brush = new SolidBrush(Color.FromArgb(120, Color.Gray));
  213. private bool IsMouseOver { get; set; }
  214. private bool IsMouseOverOnImage { get; set; }
  215. private bool IsMouseDown { get; set; }
  216. private Point MouseDownPoint { get; set; }
  217. private Point MouseMovePrecives { get; set; }
  218. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  219. {
  220. //最终呈现的选择区域
  221. if (HasPicture)
  222. {
  223. Graphics g = e.Graphics;
  224. g.FillRectangle(brush, RectangleSelected);
  225. g.DrawRectangle(Pens.LightSkyBlue, RectangleSelected);
  226. }
  227. }
  228. private void userButton2_Click(object sender, EventArgs e)
  229. {
  230. if (HasPicture)
  231. {
  232. DialogResult = DialogResult.OK;
  233. }
  234. else
  235. {
  236. DialogResult = DialogResult.Cancel;
  237. }
  238. }
  239. private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  240. {
  241. //if (HasPicture)
  242. //{
  243. // if (e.KeyCode == Keys.Left)
  244. // {
  245. // Point point = new Point(MouseMovePrecives.X - 1, MouseMovePrecives.Y);
  246. // MoveByPoint(point);
  247. // SetImgaeMiniShow();
  248. // }
  249. // else if(e.KeyCode==Keys.Right)
  250. // {
  251. // Point point = new Point(MouseMovePrecives.X + 1, MouseMovePrecives.Y);
  252. // MoveByPoint(point);
  253. // SetImgaeMiniShow();
  254. // }
  255. // else if(e.KeyCode==Keys.Up)
  256. // {
  257. // Point point = new Point(MouseMovePrecives.X, MouseMovePrecives.Y - 1);
  258. // MoveByPoint(point);
  259. // SetImgaeMiniShow();
  260. // }
  261. // else if(e.KeyCode==Keys.Down)
  262. // {
  263. // Point point = new Point(MouseMovePrecives.X, MouseMovePrecives.Y + 1);
  264. // MoveByPoint(point);
  265. // SetImgaeMiniShow();
  266. // }
  267. // pictureBox1.Focus();
  268. //}
  269. }
  270. /// <summary>
  271. /// 获取指定大小的图片,该图片将会按照比例压缩
  272. /// </summary>
  273. /// <param name="size">图片的横向分辨率</param>
  274. /// <returns>缩放后的图形</returns>
  275. public Bitmap GetSpecifiedSizeImage(int size)
  276. {
  277. if(HasPicture)
  278. {
  279. return GetSpecicalSizeFromImage(size, RectangleRestore());
  280. }
  281. else
  282. {
  283. return null;
  284. }
  285. }
  286. }
  287. }