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.

991 lines
40 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using Customize.Controls;
  14. namespace ShenTun.ImageCollection.Common
  15. {
  16. public partial class ImageView : UserControl
  17. {
  18. private static int rotaNum = 0; // 旋转角度
  19. private static int pointRadis = 12; // 裁切点大小
  20. private static Boolean clipMode = false; // 是否开启裁切
  21. private static Boolean textMode = false; // 是否开启标注
  22. private static Pen clipPen = new Pen(Color.Red, 10); // 裁切颜色
  23. private static string imagePath = null; // 图像路径
  24. private static Image sourceImage = null; // 初始输入图像源
  25. private static Image outputImage = null; // 初始输出图像源 未使用
  26. private static PictureBox currentPictureBox = null; // 当前图像容器
  27. private static Panel currentImageBox = null; // 当前图像panel
  28. private static Panel currentClipPanel = null; // 裁切平面
  29. private static ExtendedTextPanel currentInputPanel = null; // 文字输入框
  30. private static Point one = new Point(0, 0); // 裁切调整坐标1
  31. private static Point two = new Point(0, 0); // 裁切调整坐标2
  32. private static Point three = new Point(0, 0); // 裁切调整坐标3
  33. private static Point four = new Point(0, 0); // 裁切调整坐标4
  34. private static PictureBox OneButton = new PictureBox(); // 裁切调整点1
  35. private static PictureBox TwoButton = new PictureBox(); // 裁切调整点2
  36. private static PictureBox ThreeButton = new PictureBox(); // 裁切调整点3
  37. private static PictureBox FourButton = new PictureBox(); // 裁切调整点4
  38. private static System.Drawing.Rectangle clipRect = new System.Drawing.Rectangle(0, 0, 0, 0); // 裁切矩形信息
  39. private bool hasInit=false;
  40. private SelectImageInfo selectImage=null;
  41. private ApiService apiService = new ApiService();
  42. private bool isShowList = false;
  43. public Point mouseDownPoint; // 记录鼠标点击坐标
  44. public Point textPoint;
  45. public bool isSelected = false; // 是否点击图像
  46. public bool isDrawing = false; // 是否点击图像
  47. public bool isClicked = false; // 是否点击裁切矩形
  48. public bool isClickedOne = false; // 是否点解裁切矩形调整点1
  49. public bool isClickedTwo = false; // 是否点解裁切矩形调整点2
  50. public bool isClickedThree = false; // 是否点解裁切矩形调整点3
  51. public bool isClickedFour = false; // 是否点解裁切矩形调整点4
  52. public ImageView()
  53. {
  54. InitializeComponent();
  55. }
  56. public void LoadImageAndInit(MemoryStream mstr)
  57. {
  58. init(mstr, this.ImageBox, this.pictureBox);
  59. }
  60. public void LoadImageAndInit(MemoryStream mstr, SelectImageInfo imageInfo)
  61. {
  62. isShowList = true;
  63. init(mstr, this.ImageBox, this.pictureBox);
  64. selectImage = imageInfo;
  65. }
  66. /**
  67. *
  68. */
  69. public void init(MemoryStream mstr,Panel currentImage, PictureBox pictureBox)
  70. {
  71. //imagePath = path.Trim();
  72. sourceImage = ImageHelper.GetImage(mstr);
  73. currentImageBox = currentImage;
  74. if (!hasInit)
  75. {
  76. /************************按钮事件**************************/
  77. this.reset.Click += new System.EventHandler(this.reset_Click);
  78. this.rotaLeft.Click += new System.EventHandler(this.rotaLeft_Click);
  79. this.roatRight.Click += new System.EventHandler(this.roatRight_Click);
  80. this.rota90.Click += new System.EventHandler(this.rota90_Click);
  81. this.rota180.Click += new System.EventHandler(this.rota180_Click);
  82. this.mirror.Click += new System.EventHandler(this.mirror_Click);
  83. this.vertical.Click += new System.EventHandler(this.vertical_Click);
  84. this.clip.Click += new System.EventHandler(this.clip_Click);
  85. this.textImport.Click += new System.EventHandler(this.textImport_Click);
  86. this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
  87. this.btnClose.Click += new System.EventHandler(this.btnCancel_Click);
  88. this.btnList.Click += new System.EventHandler(this.btnList_Click);
  89. }
  90. currentPictureBox = pictureBox;
  91. currentPictureBox.Location = new Point(0, 0);
  92. currentPictureBox.Width = 0;
  93. currentPictureBox.Height = 0;
  94. currentPictureBox.Image = sourceImage;
  95. currentPictureBox.Width = sourceImage.Width;
  96. currentPictureBox.Height = sourceImage.Height;
  97. currentPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
  98. //imagePath = path.Trim();
  99. if (!hasInit)
  100. {
  101. this.SizeChanged += new EventHandler(this.ImageEdit_ResizeBegin);
  102. currentPictureBox.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseWheel);
  103. currentPictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
  104. currentPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
  105. currentPictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseUp);
  106. }
  107. /************************裁切框组件**************************/
  108. OneButton.Size = new Size(pointRadis, pointRadis);
  109. TwoButton.Size = new Size(pointRadis, pointRadis);
  110. ThreeButton.Size = new Size(pointRadis, pointRadis);
  111. FourButton.Size = new Size(pointRadis, pointRadis);
  112. OneButton.BackColor = Color.Red;
  113. TwoButton.BackColor = Color.Red;
  114. ThreeButton.BackColor = Color.Red;
  115. FourButton.BackColor = Color.Red;
  116. OneButton.Visible = clipMode;
  117. TwoButton.Visible = clipMode;
  118. ThreeButton.Visible = clipMode;
  119. FourButton.Visible = clipMode;
  120. FourButton.TabIndex = 10000;
  121. if (!hasInit)
  122. {
  123. currentImageBox.Controls.Add(OneButton);
  124. currentImageBox.Controls.Add(TwoButton);
  125. currentImageBox.Controls.Add(ThreeButton);
  126. currentImageBox.Controls.Add(FourButton);
  127. currentImageBox.Controls.Add(pictureBox);
  128. }
  129. currentClipPanel = this.clipPanel;
  130. currentClipPanel.BackColor = Color.Transparent;
  131. currentClipPanel.Visible = false;
  132. currentClipPanel.Location = new Point(0, 0);
  133. currentClipPanel.Width = 0;
  134. currentClipPanel.Height = 0;
  135. if (!hasInit)
  136. {
  137. currentClipPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.clip_MouseDown);
  138. currentClipPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.clip_MouseMove);
  139. currentClipPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.clip_MouseUp);
  140. OneButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.One_MouseDown);
  141. OneButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.One_MouseMove);
  142. OneButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.One_MouseUp);
  143. TwoButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Two_MouseDown);
  144. TwoButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Two_MouseMove);
  145. TwoButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Two_MouseUp);
  146. ThreeButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Three_MouseDown);
  147. ThreeButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Three_MouseMove);
  148. ThreeButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Three_MouseUp);
  149. FourButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Four_MouseDown);
  150. FourButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Four_MouseMove);
  151. FourButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Four_MouseUp);
  152. }
  153. clipRect.X = 0;
  154. clipRect.Y = 0;
  155. clipRect.Width = currentPictureBox.Image.Width;
  156. clipRect.Height = currentPictureBox.Image.Height;
  157. resetImageToPanel();
  158. hasInit = true;
  159. this.btnList.Visible = isShowList;
  160. }
  161. public void ImageClear()
  162. {
  163. if (currentPictureBox.Image != null)
  164. {
  165. currentPictureBox.Image.Dispose();
  166. }
  167. }
  168. private void ImageEdit_ResizeBegin(object sender, EventArgs e)
  169. {
  170. resetImageToPanel();
  171. }
  172. private static void resetImageToPanel()
  173. {
  174. int windowWidth = currentImageBox.Width;
  175. int windowHeight = currentImageBox.Height;
  176. int imageWidth = clipRect.Width;
  177. int imageHeight = clipRect.Height;
  178. if (imageWidth >= imageHeight)
  179. {
  180. if (imageWidth > windowWidth)
  181. {
  182. double scaleHeight = (double)windowWidth / imageWidth * imageHeight;
  183. currentPictureBox.Width = windowWidth;
  184. currentPictureBox.Height = (int)scaleHeight;
  185. }
  186. else
  187. {
  188. currentPictureBox.Width = imageWidth;
  189. currentPictureBox.Height = imageHeight;
  190. }
  191. }
  192. else
  193. {
  194. if (imageHeight > windowHeight)
  195. {
  196. double scaleWidth = (double)windowHeight / imageHeight * imageWidth;
  197. currentPictureBox.Height = windowHeight;
  198. currentPictureBox.Width = (int)scaleWidth;
  199. }
  200. else
  201. {
  202. currentPictureBox.Width = imageWidth;
  203. currentPictureBox.Height = imageHeight;
  204. }
  205. }
  206. int X = (windowWidth - currentPictureBox.Width) / 2;
  207. int Y = (windowHeight - currentPictureBox.Height) / 2;
  208. currentPictureBox.Location = new Point(X, Y);
  209. }
  210. private void btnCancel_Click(object sender, EventArgs e)
  211. {
  212. if (currentPictureBox.Image != null)
  213. {
  214. currentPictureBox.Image.Dispose();
  215. }
  216. if (this.Parent is Form)
  217. {
  218. if (((Form)this.Parent).Name.Equals("FrmImage"))
  219. {
  220. ((Form)this.Parent).DialogResult = DialogResult.Cancel;
  221. }
  222. }
  223. if (this.Parent is SplitterPanel)
  224. {
  225. if(this.Parent.Parent.Parent is Form)
  226. {
  227. if (((Form)this.Parent.Parent.Parent).Name.Equals("FrmImageList"))
  228. {
  229. ((Form)this.Parent.Parent.Parent).DialogResult = DialogResult.Cancel;
  230. }
  231. }
  232. }
  233. }
  234. private void btnList_Click(object sender, EventArgs e)
  235. {
  236. if (this.Parent is SplitterPanel)
  237. {
  238. if (this.Parent.Parent is SplitContainer)
  239. {
  240. ((SplitContainer)this.Parent.Parent).Panel1Collapsed = !((SplitContainer)this.Parent.Parent).Panel1Collapsed;
  241. }
  242. }
  243. }
  244. private async void btnSave_Click(object sender, EventArgs e)
  245. {
  246. if (clipMode)
  247. {
  248. this.clip.BackColor = Color.Transparent;
  249. this.clip.ForeColor = Color.Black;
  250. this.overPainting();
  251. clipMode = false;
  252. }
  253. if (textMode)
  254. {
  255. writeToImage();
  256. textMode = false;
  257. }
  258. if(this.Parent is Form)
  259. {
  260. ImageData.Image = (Bitmap)currentPictureBox.Image.Clone();
  261. if (currentPictureBox.Image != null)
  262. {
  263. currentPictureBox.Image.Dispose();
  264. }
  265. if (((Form)this.Parent).Name.Equals("FrmImage"))
  266. {
  267. ((Form)this.Parent).DialogResult = DialogResult.OK;
  268. }
  269. }
  270. if (this.Parent is SplitterPanel)
  271. {
  272. if (this.Parent.Parent.Parent is Form)
  273. {
  274. if (((Form)this.Parent.Parent.Parent).Name.Equals("FrmImageList"))
  275. {
  276. this.btnSave.Enabled = false;
  277. if (File.Exists(selectImage.Image.localPathName))
  278. {
  279. File.Delete(selectImage.Image.localPathName);
  280. }
  281. var oldImg = (Bitmap)currentPictureBox.Image.Clone();
  282. Bitmap img = new Bitmap(oldImg);
  283. Graphics draw = Graphics.FromImage(img);
  284. draw.DrawImage(oldImg, 0, 0, oldImg.Width, oldImg.Height);
  285. oldImg.Dispose();
  286. int iPos = selectImage.Image.localPathName.LastIndexOf("\\");
  287. string localFilename = selectImage.Image.localPathName.Substring(iPos + 1);
  288. using (MemoryStream stream = new MemoryStream())
  289. {
  290. img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  291. await apiService.UploadImage(stream, selectImage);
  292. }
  293. img.Save(selectImage.Image.localPathName, System.Drawing.Imaging.ImageFormat.Jpeg);
  294. img.Dispose();
  295. this.btnSave.Enabled = true;
  296. }
  297. }
  298. }
  299. }
  300. private void reset_Click(object sender, EventArgs e)
  301. {
  302. if (!clipMode)
  303. {
  304. rotaNum = 0;
  305. currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
  306. resetImageToPanel();
  307. }
  308. }
  309. private void rotaLeft_Click(object sender, EventArgs e)
  310. {
  311. currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 5);
  312. }
  313. private void roatRight_Click(object sender, EventArgs e)
  314. {
  315. currentPictureBox.Image = RotateImage(sourceImage, rotaNum += 5);
  316. }
  317. private void mirror_Click(object sender, EventArgs e)
  318. {
  319. currentPictureBox.Image = turnOverImage(currentPictureBox.Image, "mirror");
  320. }
  321. private void vertical_Click(object sender, EventArgs e)
  322. {
  323. currentPictureBox.Image = turnOverImage(currentPictureBox.Image, "vertical");
  324. }
  325. private void rota90_Click(object sender, EventArgs e)
  326. {
  327. currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 90);
  328. }
  329. private void rota180_Click(object sender, EventArgs e)
  330. {
  331. currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 180);
  332. }
  333. private void writeToImage()
  334. {
  335. if (currentInputPanel == null) return;
  336. if (currentInputPanel.TransTextBox.Text.Length == 0) return;
  337. Graphics g = Graphics.FromImage(currentPictureBox.Image);//定义一个GDI+
  338. SolidBrush drawBrush = new SolidBrush(currentInputPanel.TransTextBox.ForeColor);//定义一个画笔,在这里我读取了textBox1的字体颜色
  339. Font drawFont = new Font(currentInputPanel.TransTextBox.Font.FontFamily, currentInputPanel.TransTextBox.Font.Size);//定义一个font,设置字的样式,大小,都是读取textBox1的值<br>         //往图片上写字。这里需要注意的是Point,因为在图片加载的时候你可能采取了一定的压缩或者放大,所以要乘以压缩比例scaling
  340. g.DrawString(currentInputPanel.TransTextBox.Text, drawFont, drawBrush, new Point(currentInputPanel.Location.X, currentInputPanel.Location.Y));
  341. currentPictureBox.Controls.Remove(currentInputPanel);//然后移除添加的那个textBox1
  342. }
  343. private void clip_Click(object sender, EventArgs e)
  344. {
  345. clipMode = !clipMode;
  346. if (textMode)
  347. {
  348. writeToImage();
  349. textMode = false;
  350. this.textImport.BackColor = Color.Transparent;
  351. this.textImport.ForeColor = Color.Black;
  352. }
  353. if (clipMode)
  354. {
  355. this.clip.BackColor = Color.Blue;
  356. this.clip.ForeColor = Color.White;
  357. this.beginPainting();
  358. }
  359. else
  360. {
  361. this.clip.BackColor = Color.Transparent;
  362. this.clip.ForeColor = Color.Black;
  363. this.overPainting();
  364. }
  365. }
  366. private void textImport_Click(object sender, EventArgs e)
  367. {
  368. textMode = !textMode;
  369. if (clipMode)
  370. {
  371. this.clip.BackColor = Color.Transparent;
  372. this.clip.ForeColor = Color.Black;
  373. this.overPainting();
  374. }
  375. if (textMode)
  376. {
  377. this.textImport.BackColor = Color.Blue;
  378. this.textImport.ForeColor = Color.White;
  379. }
  380. else
  381. {
  382. this.textImport.BackColor = Color.Transparent;
  383. this.textImport.ForeColor = Color.Black;
  384. writeToImage();
  385. }
  386. }
  387. /************************图像鼠标事件**************************/
  388. private void pictureBox_MouseWheel(object sender, MouseEventArgs e)
  389. {
  390. if (!clipMode && currentPictureBox.ClientRectangle.Contains(e.Location))
  391. {
  392. ((HandledMouseEventArgs)e).Handled = true;
  393. currentPictureBox.Focus();
  394. float zoomChange = e.Delta > 0 ? 2f : 0.5f;
  395. // 计算缩放后图片的大小
  396. int newWidth = (int)(currentPictureBox.Width * zoomChange);
  397. int newHeight = (int)(currentPictureBox.Height * zoomChange);
  398. int newX = 0;
  399. int newY = 0;
  400. if (e.Delta > 0)
  401. {
  402. newX = (int)(currentPictureBox.Left - e.X);
  403. newY = (int)(currentPictureBox.Top - e.Y);
  404. }
  405. else
  406. {
  407. newX = (int)(currentPictureBox.Left + e.X / 2);
  408. newY = (int)(currentPictureBox.Top + e.Y / 2);
  409. }
  410. currentPictureBox.Size = new Size(newWidth, newHeight);
  411. currentPictureBox.Location = new Point(newX, newY);
  412. }
  413. }
  414. private void pictureBox_MouseDown(object sender, MouseEventArgs e)
  415. {
  416. if (!clipMode)
  417. {
  418. if (currentImageBox != null)
  419. {
  420. currentImageBox.Focus();
  421. }
  422. if (e.Button == MouseButtons.Left)
  423. {
  424. if (textMode)
  425. {
  426. mouseDownPoint.X = e.Location.X;
  427. mouseDownPoint.Y = e.Location.Y;
  428. Cursor.Current = Cursors.Cross;
  429. isDrawing = true;
  430. this.addTextInput(e);
  431. }
  432. else
  433. {
  434. mouseDownPoint.X = Cursor.Position.X;
  435. mouseDownPoint.Y = Cursor.Position.Y;
  436. Cursor.Current = Cursors.SizeAll;
  437. isSelected = true;
  438. }
  439. }
  440. }
  441. }
  442. private void pictureBox_MouseMove(object sender, MouseEventArgs e)
  443. {
  444. if (!clipMode)
  445. {
  446. if (isDrawing)
  447. {
  448. this.resizeTextInput(e);
  449. mouseDownPoint.X = e.Location.X;
  450. mouseDownPoint.Y = e.Location.Y;
  451. }
  452. else if (isSelected)
  453. {
  454. currentPictureBox.Left = currentPictureBox.Left + (Cursor.Position.X - mouseDownPoint.X);
  455. currentPictureBox.Top = currentPictureBox.Top + (Cursor.Position.Y - mouseDownPoint.Y);
  456. mouseDownPoint.X = Cursor.Position.X;
  457. mouseDownPoint.Y = Cursor.Position.Y;
  458. }
  459. }
  460. }
  461. private void pictureBox_MouseUp(object sender, MouseEventArgs e)
  462. {
  463. if (textMode)
  464. {
  465. this.importTextInput();
  466. }
  467. isSelected = false;
  468. isDrawing = false;
  469. }
  470. /*************************添加输入框*************************/
  471. private void addTextInput(MouseEventArgs e)
  472. {
  473. Debug.WriteLine("addTextInput");
  474. ExtendedTextPanel inputPanel = new ExtendedTextPanel();
  475. currentInputPanel = inputPanel;
  476. currentInputPanel.Opacity = 0;
  477. currentInputPanel.Mode = "text";
  478. currentPictureBox.Controls.Add(inputPanel);
  479. currentInputPanel.Size = new Size(0, 0);
  480. currentInputPanel.Location = new Point(e.Location.X, e.Location.Y);
  481. }
  482. private void resizeTextInput(MouseEventArgs e)
  483. {
  484. if (currentInputPanel != null)
  485. {
  486. int dtW = e.Location.X - mouseDownPoint.X;
  487. int dtH = e.Location.Y - mouseDownPoint.Y;
  488. currentInputPanel.Width += dtW;
  489. currentInputPanel.Height += dtH;
  490. }
  491. }
  492. private void importTextInput()
  493. {
  494. if (currentInputPanel == null)
  495. {
  496. return;
  497. }
  498. // 太小的不创建
  499. if (currentInputPanel.Width < 10 || currentInputPanel.Height < 10)
  500. {
  501. currentPictureBox.Controls.Remove(currentInputPanel);
  502. currentInputPanel = null;
  503. return;
  504. }
  505. Debug.WriteLine("importTextInput");
  506. currentInputPanel.showTransTextBox();
  507. // currentInputPanel.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
  508. // textBox.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
  509. // textBox.MouseClick += new MouseEventHandler(txt_MouseClick); // 鼠标点击事件
  510. }
  511. private void txt_LostFocus(object sender, EventArgs e)
  512. {
  513. Debug.WriteLine("txt_LostFocus");
  514. TransTextBox textBox = (TransTextBox)sender;
  515. }
  516. private void txt_GotFocus(object sender, EventArgs e)
  517. {
  518. Debug.WriteLine("txt_GotFocus");
  519. TransTextBox textBox = (TransTextBox)sender;
  520. ExtendedPanel inputPanel = (ExtendedPanel)textBox.Parent;
  521. }
  522. private void txt_MouseClick(object sender, EventArgs e)
  523. {
  524. Debug.WriteLine("txt_MouseClick");
  525. TransTextBox textBox = (TransTextBox)sender;
  526. }
  527. /*************************裁切平面鼠标事件*************************/
  528. private void clip_MouseDown(object sender, MouseEventArgs e)
  529. {
  530. if (e.Button == MouseButtons.Left)
  531. {
  532. mouseDownPoint.X = Cursor.Position.X;
  533. mouseDownPoint.Y = Cursor.Position.Y;
  534. isClicked = true;
  535. }
  536. }
  537. private void clip_MouseMove(object sender, MouseEventArgs e)
  538. {
  539. if (isClicked)
  540. {
  541. int dotaX = mouseDownPoint.X - Cursor.Position.X;
  542. int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
  543. currentClipPanel.Left = currentClipPanel.Left - dotaX;
  544. currentClipPanel.Top = currentClipPanel.Top - dotaY;
  545. // 刷新第一、二、三、四个点位置
  546. int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
  547. int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
  548. OneButton.Location = new Point(newOneX, currentClipPanel.Location.Y - pointRadis / 2);
  549. ThreeButton.Location = new Point(newOneX, currentClipPanel.Location.Y + currentClipPanel.Height - pointRadis / 2);
  550. TwoButton.Location = new Point(currentClipPanel.Location.X + currentClipPanel.Width - pointRadis / 2, newTwoY);
  551. FourButton.Location = new Point(currentClipPanel.Location.X - pointRadis / 2, newTwoY);
  552. mouseDownPoint.X = Cursor.Position.X;
  553. mouseDownPoint.Y = Cursor.Position.Y;
  554. }
  555. }
  556. private void clip_MouseUp(object sender, MouseEventArgs e)
  557. {
  558. isClicked = false;
  559. }
  560. /*************************第一个点鼠标事件*************************/
  561. private void One_MouseDown(object sender, MouseEventArgs e)
  562. {
  563. if (e.Button == MouseButtons.Left)
  564. {
  565. mouseDownPoint.X = Cursor.Position.X;
  566. mouseDownPoint.Y = Cursor.Position.Y;
  567. Cursor.Current = Cursors.SizeNS;
  568. isClickedOne = true;
  569. }
  570. }
  571. private void One_MouseMove(object sender, MouseEventArgs e)
  572. {
  573. if (isClickedOne)
  574. {
  575. int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
  576. if (ThreeButton.Location.Y - OneButton.Location.Y > 50 || dotaY > 0)
  577. {
  578. // 计算高度和位置
  579. currentClipPanel.Height = currentClipPanel.Height + dotaY;
  580. int newY = currentClipPanel.Location.Y - dotaY;
  581. currentClipPanel.Location = new Point(currentClipPanel.Location.X, newY);
  582. int newPointY = OneButton.Location.Y - dotaY;
  583. OneButton.Location = new Point(OneButton.Location.X, newPointY);
  584. // 刷新第二、四个点位置
  585. int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
  586. TwoButton.Location = new Point(TwoButton.Location.X, newTwoY);
  587. FourButton.Location = new Point(FourButton.Location.X, newTwoY);
  588. mouseDownPoint.X = Cursor.Position.X;
  589. mouseDownPoint.Y = Cursor.Position.Y;
  590. }
  591. }
  592. }
  593. private void One_MouseUp(object sender, MouseEventArgs e)
  594. {
  595. isClickedOne = false;
  596. }
  597. /*************************第二个点鼠标事件*************************/
  598. private void Two_MouseDown(object sender, MouseEventArgs e)
  599. {
  600. if (e.Button == MouseButtons.Left)
  601. {
  602. Cursor.Current = Cursors.SizeWE;
  603. mouseDownPoint.X = Cursor.Position.X;
  604. mouseDownPoint.Y = Cursor.Position.Y;
  605. isClickedTwo = true;
  606. }
  607. }
  608. private void Two_MouseMove(object sender, MouseEventArgs e)
  609. {
  610. if (isClickedTwo)
  611. {
  612. int dotaX = mouseDownPoint.X - Cursor.Position.X;
  613. if (TwoButton.Location.X - FourButton.Location.X > 50 || dotaX < 0)
  614. {
  615. // 计算宽度和位置
  616. currentClipPanel.Width -= dotaX;
  617. int newPointX = TwoButton.Location.X - dotaX;
  618. TwoButton.Location = new Point(newPointX, TwoButton.Location.Y);
  619. // 刷新第一、三个点位置
  620. int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
  621. OneButton.Location = new Point(newOneX, OneButton.Location.Y);
  622. ThreeButton.Location = new Point(newOneX, ThreeButton.Location.Y);
  623. mouseDownPoint.X = Cursor.Position.X;
  624. mouseDownPoint.Y = Cursor.Position.Y;
  625. }
  626. }
  627. }
  628. private void Two_MouseUp(object sender, MouseEventArgs e)
  629. {
  630. isClickedTwo = false;
  631. }
  632. /*************************第三个点鼠标事件*************************/
  633. private void Three_MouseDown(object sender, MouseEventArgs e)
  634. {
  635. if (e.Button == MouseButtons.Left)
  636. {
  637. Cursor.Current = Cursors.SizeNS;
  638. mouseDownPoint.X = Cursor.Position.X;
  639. mouseDownPoint.Y = Cursor.Position.Y;
  640. isClickedThree = true;
  641. }
  642. }
  643. private void Three_MouseMove(object sender, MouseEventArgs e)
  644. {
  645. if (isClickedThree)
  646. {
  647. int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
  648. if (ThreeButton.Location.Y - OneButton.Location.Y > 50 || dotaY < 0)
  649. {
  650. // 计算高度和位置
  651. currentClipPanel.Height = currentClipPanel.Height - dotaY;
  652. int newPointY = ThreeButton.Location.Y - dotaY;
  653. ThreeButton.Location = new Point(ThreeButton.Location.X, newPointY);
  654. // 刷新第二、四个点位置
  655. int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
  656. TwoButton.Location = new Point(TwoButton.Location.X, newTwoY);
  657. FourButton.Location = new Point(FourButton.Location.X, newTwoY);
  658. mouseDownPoint.X = Cursor.Position.X;
  659. mouseDownPoint.Y = Cursor.Position.Y;
  660. }
  661. }
  662. }
  663. private void Three_MouseUp(object sender, MouseEventArgs e)
  664. {
  665. isClickedThree = false;
  666. }
  667. /*************************第四个点鼠标事件*************************/
  668. private void Four_MouseDown(object sender, MouseEventArgs e)
  669. {
  670. if (e.Button == MouseButtons.Left)
  671. {
  672. Cursor.Current = Cursors.SizeWE;
  673. mouseDownPoint.X = Cursor.Position.X;
  674. mouseDownPoint.Y = Cursor.Position.Y;
  675. isClickedFour = true;
  676. }
  677. }
  678. private void Four_MouseMove(object sender, MouseEventArgs e)
  679. {
  680. if (isClickedFour)
  681. {
  682. // 计算宽度和位置
  683. int dotaX = mouseDownPoint.X - Cursor.Position.X;
  684. if (TwoButton.Location.X - FourButton.Location.X > 50 || dotaX > 0)
  685. {
  686. currentClipPanel.Width += dotaX;
  687. int newX = currentClipPanel.Location.X - dotaX;
  688. currentClipPanel.Location = new Point(newX, currentClipPanel.Location.Y);
  689. int newPointX = FourButton.Location.X - dotaX;
  690. FourButton.Location = new Point(newPointX, FourButton.Location.Y);
  691. // 刷新第一、三个点位置
  692. int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
  693. OneButton.Location = new Point(newOneX, OneButton.Location.Y);
  694. ThreeButton.Location = new Point(newOneX, ThreeButton.Location.Y);
  695. mouseDownPoint.X = Cursor.Position.X;
  696. mouseDownPoint.Y = Cursor.Position.Y;
  697. }
  698. }
  699. }
  700. private void Four_MouseUp(object sender, MouseEventArgs e)
  701. {
  702. isClickedFour = false;
  703. }
  704. /***************************功能方法***********************/
  705. /**
  706. *
  707. */
  708. private void beginPainting()
  709. {
  710. Debug.WriteLine("====beginPainting====");
  711. OneButton.Visible = clipMode;
  712. TwoButton.Visible = clipMode;
  713. ThreeButton.Visible = clipMode;
  714. FourButton.Visible = clipMode;
  715. currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
  716. currentPictureBox.Refresh();
  717. // 初始化位置
  718. setDefaultClipButtonPosition();
  719. currentClipPanel.Visible = clipMode;
  720. }
  721. /**
  722. *
  723. */
  724. private void overPainting()
  725. {
  726. Debug.WriteLine("====overPainting====");
  727. OneButton.Visible = clipMode;
  728. TwoButton.Visible = clipMode;
  729. ThreeButton.Visible = clipMode;
  730. FourButton.Visible = clipMode;
  731. currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
  732. currentImageBox.Refresh();
  733. // 裁切
  734. clipBitmap();
  735. currentClipPanel.Visible = clipMode;
  736. }
  737. /**
  738. *
  739. */
  740. private static void setDefaultClipButtonPosition()
  741. {
  742. int halfButton = pointRadis / 2;
  743. // 设置拖拉按钮
  744. one.X = currentPictureBox.Location.X + currentPictureBox.Width / 2 - halfButton;
  745. one.Y = currentPictureBox.Location.Y - halfButton;
  746. OneButton.Location = one;
  747. two.X = currentPictureBox.Location.X + currentPictureBox.Width - halfButton;
  748. two.Y = currentPictureBox.Location.Y + currentPictureBox.Height / 2 - halfButton;
  749. TwoButton.Location = two;
  750. three.X = one.X;
  751. three.Y = currentPictureBox.Location.Y + currentPictureBox.Height - halfButton;
  752. ThreeButton.Location = three;
  753. four.X = currentPictureBox.Location.X - halfButton;
  754. four.Y = two.Y;
  755. FourButton.Location = four;
  756. currentClipPanel.Location = currentPictureBox.Location;
  757. currentClipPanel.Width = currentPictureBox.Width;
  758. currentClipPanel.Height = currentPictureBox.Height;
  759. Graphics graphicsClip = OneButton.CreateGraphics();
  760. graphicsClip.DrawRectangle(clipPen, clipRect);
  761. graphicsClip.Dispose();
  762. }
  763. /**
  764. *
  765. */
  766. private static void clipBitmap()
  767. {
  768. Image srcImage = currentPictureBox.Image;
  769. int X = currentClipPanel.Location.X - currentPictureBox.Location.X;
  770. int Y = currentClipPanel.Location.Y - currentPictureBox.Location.Y;
  771. int crossWidth = X < 0 ? currentClipPanel.Width + X : currentClipPanel.Width;
  772. int crossHeight = Y < 0 ? currentClipPanel.Height + Y : currentClipPanel.Height;
  773. int Width = crossWidth * srcImage.Width / currentPictureBox.Width;
  774. int Height = crossHeight * srcImage.Height / currentPictureBox.Height;
  775. X = X < 0 ? 0 : X * srcImage.Width / currentPictureBox.Width;
  776. Y = Y < 0 ? 0 : Y * srcImage.Height / currentPictureBox.Height;
  777. // 创建新图位图
  778. Bitmap bitmap = new Bitmap(Width, Height);
  779. bitmap.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
  780. // 创建作图区域
  781. Graphics graphic = Graphics.FromImage(bitmap);
  782. graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  783. graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
  784. // 截取原图相应区域写入作图区
  785. Rectangle cRect = new Rectangle(X, Y, Width, Height);
  786. graphic.DrawImage(srcImage, 0, 0, cRect, GraphicsUnit.Pixel);
  787. graphic.Dispose();
  788. /*Debug.WriteLine("SourceWidth===========>" + sourceImage.Width);
  789. Debug.WriteLine("SourceHeight===========>" + sourceImage.Height);
  790. Debug.WriteLine("Width===========>" + bitmap.Width);
  791. Debug.WriteLine("Height===========>" + bitmap.Height);*/
  792. //从作图区生成新图
  793. // currentPictureBox.Image = Image.FromHbitmap( bitmap.GetHbitmap() );
  794. /********刷新图像*********/
  795. currentPictureBox.Image = bitmap;
  796. sourceImage = bitmap;
  797. rotaNum = 0;
  798. currentImageBox.Refresh();
  799. clipRect.X = 0;
  800. clipRect.Y = 0;
  801. clipRect.Width = Width;
  802. clipRect.Height = Height;
  803. /********刷新图像*********/
  804. resetImageToPanel();
  805. }
  806. /**
  807. *
  808. */
  809. private static Bitmap RotateImage(Image image, float angle)
  810. {
  811. if (image == null)
  812. {
  813. throw new ArgumentNullException("image");
  814. }
  815. // 获取中心点
  816. float halfWidth = image.Width / 2.0f;
  817. float halfHeight = image.Height / 2.0f;
  818. // 弧度转换
  819. angle = angle % 360;
  820. double radian = angle * Math.PI / 180.0;
  821. double cos = Math.Cos(radian);
  822. double sin = Math.Sin(radian);
  823. int width = image.Width;
  824. int height = image.Height;
  825. // 计算旋转后的图像容器长宽
  826. int resultWidth = (int)(Math.Max(Math.Abs(width * cos - height * sin), Math.Abs(width * cos + height * sin)));
  827. int resultHeight = (int)(Math.Max(Math.Abs(width * sin - height * cos), Math.Abs(width * sin + height * cos)));
  828. float centerX = resultWidth / 2;
  829. float centerY = resultHeight / 2;
  830. // 设置新的图像容器
  831. Bitmap newImage = new Bitmap(resultWidth, resultHeight);
  832. newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  833. Graphics graphics = Graphics.FromImage(newImage);
  834. // 平移图像到中心点
  835. graphics.TranslateTransform(centerX, centerY);
  836. // 旋转
  837. graphics.RotateTransform(angle);
  838. // 再次平移回来
  839. graphics.TranslateTransform(-centerX, -centerY);
  840. // 将图片绘制到中心点上
  841. float resultX = centerX - halfWidth;
  842. float resultY = centerY - halfHeight;
  843. graphics.DrawImage(image, new PointF(resultX, resultY));
  844. graphics.Dispose();
  845. clipRect.X = 0;
  846. clipRect.Y = 0;
  847. clipRect.Width = resultWidth;
  848. clipRect.Height = resultHeight;
  849. resetImageToPanel();
  850. // 如果是裁切将显示裁切框
  851. /*if (clipMode)
  852. {
  853. Graphics graphicsClip = Graphics.FromImage(newImage);
  854. graphicsClip.DrawRectangle(rotaPen, clipRect);
  855. graphicsClip.Dispose();
  856. }*/
  857. /*Graphics graphicsClip = Graphics.FromImage(newImage);
  858. graphicsClip.DrawRectangle(rotaPen, clipRect);
  859. graphicsClip.Dispose();*/
  860. return newImage;
  861. }
  862. /**
  863. *
  864. */
  865. private static Image turnOverImage(Image image, string mode)
  866. {
  867. Graphics g = Graphics.FromImage(image);
  868. Rectangle rect = new Rectangle(0, 0, currentPictureBox.Image.Width, currentPictureBox.Image.Height);
  869. switch (mode)
  870. {
  871. case "mirror":
  872. image.RotateFlip(RotateFlipType.RotateNoneFlipX);
  873. break;
  874. case "vertical":
  875. image.RotateFlip(RotateFlipType.RotateNoneFlipY);
  876. break;
  877. }
  878. g.DrawImage(image, rect);
  879. g.Dispose();
  880. return image;
  881. }
  882. }
  883. }