public void Draw(Graphics g, double ratio) { if (isRect == 1) { Rectangle rectRatio = SizeableArea.rectRatio(ratio, rect); DrawRect(g, rectRatio, ratio); } else { Quadrilateral quadRatio = SizeableArea.quadRatio(ratio, quad); DrawQuad(g, quadRatio, ratio); } }
/// <summary> /// 計算框選範圍的四邊形或不規則四邊形面積。 /// </summary> public static double calQuadArea(SizeableArea inputSQ) { if (inputSQ == null) { return(0); } if (inputSQ.isRect == 1) { return(inputSQ.rect.Width * inputSQ.rect.Height); } else { return(Quadrilateral.Area(inputSQ.quad)); } }
public FormImage(int mode, Function function, FormFunction formFunction, int baseSn) { GC.Collect(); this.mode = mode; this.function = function; this.formFunction = formFunction; this.baseSn = baseSn; if (mode == 1)//mode1: imageCut { Rectangle rect = new Rectangle(R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(1).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(2).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(3).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(4).variableSn].value)); SizeableArea sa = new SizeableArea(rect, Color.Blue); listSQ.Add(sa); } else if (mode == 2) //mode2: 多個 rect { inputRectArrayString(FormMain.r8.variables[function.parameters.ElementAt(baseSn).variableSn].value); } InitializeComponent(); if (mode != 0) { setToolStripMenuItem.Visible = true; } if (mode == 3 || mode == 4) { rotateValue = R8.stringToDouble(FormMain.r8.variables[function.parameters.ElementAt(baseSn).variableSn].value); labelRotate.Visible = true; numericUpDownRotate.Visible = true; numericUpDownRotate.Value = (decimal)rotateValue; if (mode == 4) { Rectangle rect = new Rectangle(R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(baseSn + 1).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(baseSn + 2).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(baseSn + 3).variableSn].value) , R8.stringToInt(FormMain.r8.variables[function.parameters.ElementAt(baseSn + 4).variableSn].value)); SizeableArea sa = new SizeableArea(rect, Color.Blue); listSQ.Add(sa); } } }
private void contextMenuStrip_itemClicked(object sender, ToolStripItemClickedEventArgs e) { Point oriPoint = new Point((int)(mouseRightDownPointX / zoomRate), (int)(mouseRightDownPointY / zoomRate)); switch (e.ClickedItem.Text) { case "Add": { int w = 100; int h = 100; if (listSQ.Count > 0) { w = listSQ.ElementAt(listSQ.Count - 1).rect.Width; h = listSQ.ElementAt(listSQ.Count - 1).rect.Height; } Rectangle rect = new Rectangle(oriPoint.X, oriPoint.Y, w, h); SizeableArea sa = new SizeableArea(rect, Color.Blue); listSQ.Add(sa); pictureBoxImage.Invalidate(); } break; case "Remove": { for (int i = listSQ.Count - 1; i > -1; i--) { SizeableArea sq = listSQ.ElementAt(i); if (sq.isRect == 1) { if (sq.rect.Contains(new Point(oriPoint.X, oriPoint.Y))) { listSQ.Remove(sq); break; } } } pictureBoxImage.Invalidate(); } break; case "Clear All": listSQ.Clear(); pictureBoxImage.Invalidate(); break; } }
public FormImage(int mode) { GC.Collect(); this.mode = mode; if (mode == 1) { Rectangle rect = new Rectangle(100, 100, 100, 100); SizeableArea sa = new SizeableArea(rect, Color.Blue); listSQ.Add(sa); } InitializeComponent(); if (mode == 3 || mode == 4) { labelRotate.Visible = true; numericUpDownRotate.Visible = true; } }
private string outputRectArrayString() { StringBuilder str = new StringBuilder(); //格式: /* * [ * {"x": "725", "y": "15", "width": "110", "height": "110"}, * {"x": "20", "y": "25", "width": "110", "height": "110"}, * {"x": "732", "y": "485", "width": "110", "height": "110"}, * {"x": "25", "y": "490", "width": "110", "height": "110"} * ] */ str.Append("["); bool isFirst = true; for (int i = 0; i < listSQ.Count; i++) { SizeableArea sq = listSQ.ElementAt(i); if (sq.isRect == 1) { if (isFirst) { isFirst = false; } else { str.Append(","); } str.Append("{\"x\": \"" + sq.rect.X + "\", \"y\": \"" + sq.rect.Y + "\", \"width\": \"" + sq.rect.Width + "\", \"height\": \"" + sq.rect.Height + "\"}"); } } str.Append("]"); return(str.ToString()); }
private void pictureBoxImage_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right && mode == 2) { contextMenuStrip = new ContextMenuStrip(); contextMenuStrip.Items.Add("Add"); contextMenuStrip.Items.Add("Remove"); contextMenuStrip.Items.Add("Clear All"); Point point = PointToClient(Cursor.Position); //Point point = PointToScreen(Cursor.Position); //Point point = PointToClient(new Point(e.X, e.Y)); //Point point = PointToClient(Form.MousePosition); contextMenuStrip.ItemClicked += contextMenuStrip_itemClicked; mouseRightDownPointX = e.X; mouseRightDownPointY = e.Y; contextMenuStrip.Show(this, point); return; } isMouseDown = 1; if (listSQ.Count == 0) { return; } isQuadMove = 1; List <SizeableArea> listMoveSQ = new List <SizeableArea>(); // 算原圖 Size 的位置 Point oriPoint = new Point((int)(e.X / zoomRate), (int)(e.Y / zoomRate)); foreach (SizeableArea sq in listSQ) { sq.isMove = 0; if (sq.isRect == 1) { if (sq.rect.Contains(new Point(oriPoint.X, oriPoint.Y))) { listMoveSQ.Add(sq); } } else { if (sq.quad.Contains(new Point(oriPoint.X, oriPoint.Y))) { listMoveSQ.Add(sq); } } sq.nodeSelected = SizeableArea.PosSizableQuad.None; sq.nodeSelected = sq.GetNodeSelectable(oriPoint); if (sq.nodeSelected != SizeableArea.PosSizableQuad.None) { isQuadMove = 0; isAction = 1; } sq.oldQuad = new Quadrilateral(sq.quad.GetPointArray()); sq.oldRect = new Rectangle(sq.rect.X, sq.rect.Y, sq.rect.Width, sq.rect.Height); sq.oldX = oriPoint.X; sq.oldY = oriPoint.Y; } if (isQuadMove == 1) { //double size = pictureBoxImage.Width * pictureBoxImage.Height * 100; //這東西會 overflow......換下面那行 double size = double.MaxValue; List <SizeableArea> moveSQ = new List <SizeableArea>(); //string str = ""; foreach (SizeableArea sq in listMoveSQ) { //str += " " + sq.isRect; if (SizeableArea.calQuadArea(sq) < size) { moveSQ.Clear(); moveSQ.Add(sq); size = SizeableArea.calQuadArea(sq); // str += " moveSQ "; } } //this.Text = str; foreach (SizeableArea sq in moveSQ) //foreach (SizeableArea sq in listSQ) { isAction = 1; sq.isMove = 1; //this.Text = "MouseDown sq" + sq.rect + " is move"; break; } } }
private int inputRectArrayString(string str) { if (str == null || str.Length < 2) //json array 至少會有前後刮符,至少有2個字元 { return(-1); } /* * [ * {"x": "725", "y": "15", "width": "110", "height": "110"}, * {"x": "20", "y": "25", "width": "110", "height": "110"}, * {"x": "732", "y": "485", "width": "110", "height": "110"}, * {"x": "25", "y": "490", "width": "110", "height": "110"} * ] */ //MessageBox.Show(str); //Rectangle rect = new Rectangle(); //string 避免 user 亂填導致 error , try catch 一下 try { int start = str.IndexOf("{"); if (start == -1) { return(-2); } int end = str.IndexOf("}"); string targetStr = str.Substring(start, end - start); string[] rectStrings; string objectString; int subStart, subEnd; string key, value; while (start != -1) { end = str.IndexOf("}", start + 1); targetStr = str.Substring(start, end - start + 1); //MessageBox.Show(targetStr); start = str.IndexOf("{", start + 1); rectStrings = targetStr.Split(','); Rectangle rect = new Rectangle(0, 0, 10, 10); for (int i = 0; i < rectStrings.Length; i++) { objectString = rectStrings[i]; //每個 objectString 裡面會有4個 " 符號 subStart = objectString.IndexOf("\""); subEnd = objectString.IndexOf("\"", subStart + 1); //冒號本身不抓所以要去頭去尾 key = objectString.Substring(subStart + 1, subEnd - subStart - 1); subStart = objectString.IndexOf("\"", subEnd + 1); subEnd = objectString.IndexOf("\"", subStart + 1); value = objectString.Substring(subStart + 1, subEnd - subStart - 1); //MessageBox.Show("key = " + key + " value = " + value); switch (key) { case "x": rect.X = R8.stringToInt(value); break; case "y": rect.Y = R8.stringToInt(value); break; case "width": rect.Width = R8.stringToInt(value); break; case "height": rect.Height = R8.stringToInt(value); break; } } SizeableArea sa = new SizeableArea(rect, Color.Blue); listSQ.Add(sa); } } catch (Exception e) { MessageBox.Show("Decode rect array string error, target string may be not a rect array." + e.ToString()); } return(1); }
public Rectangle rectRatio(double ratio) { return(SizeableArea.rectRatio(ratio, rect)); }
private Rectangle GetRect(PosSizableQuad p, double ratio) { if (isRect == 1) { Rectangle rectRatio = SizeableArea.rectRatio(ratio, rect); switch (p) { case PosSizableQuad.LeftUp: return(CreateRectSizableNode(rectRatio.X, rectRatio.Y)); case PosSizableQuad.LeftMiddle: return(CreateRectSizableNode(rectRatio.X, rectRatio.Y + rectRatio.Height / 2)); case PosSizableQuad.LeftDown: return(CreateRectSizableNode(rectRatio.X, rectRatio.Y + rectRatio.Height)); case PosSizableQuad.DownMiddle: return(CreateRectSizableNode(rectRatio.X + rectRatio.Width / 2, rectRatio.Y + rectRatio.Height)); case PosSizableQuad.RightDown: return(CreateRectSizableNode(rectRatio.X + rectRatio.Width, rectRatio.Y + rectRatio.Height)); case PosSizableQuad.RightMiddle: return(CreateRectSizableNode(rectRatio.X + rectRatio.Width, rectRatio.Y + rectRatio.Height / 2)); case PosSizableQuad.RightUp: return(CreateRectSizableNode(rectRatio.X + rectRatio.Width, rectRatio.Y)); case PosSizableQuad.UpMiddle: return(CreateRectSizableNode(rectRatio.X + rectRatio.Width / 2, rectRatio.Y)); default: return(new Rectangle()); } } else { Quadrilateral quadRatio = SizeableArea.quadRatio(ratio, quad); switch (p) { case PosSizableQuad.LeftUp: return(CreateRectSizableNode(quadRatio.LeftUp.X, quadRatio.LeftUp.Y)); case PosSizableQuad.LeftMiddle: return(CreateRectSizableNode((quadRatio.LeftUp.X + quadRatio.LeftDown.X) / 2, (quadRatio.LeftUp.Y + quadRatio.LeftDown.Y) / 2)); case PosSizableQuad.LeftDown: return(CreateRectSizableNode(quadRatio.LeftDown.X, quadRatio.LeftDown.Y)); case PosSizableQuad.DownMiddle: return(CreateRectSizableNode((quadRatio.LeftDown.X + quadRatio.RightDown.X) / 2, (quadRatio.LeftDown.Y + quadRatio.RightDown.Y) / 2)); case PosSizableQuad.RightDown: return(CreateRectSizableNode(quadRatio.RightDown.X, quadRatio.RightDown.Y)); case PosSizableQuad.RightMiddle: return(CreateRectSizableNode((quadRatio.RightDown.X + quadRatio.RightUp.X) / 2, (quadRatio.RightDown.Y + quadRatio.RightUp.Y) / 2)); case PosSizableQuad.RightUp: return(CreateRectSizableNode(quadRatio.RightUp.X, quadRatio.RightUp.Y)); case PosSizableQuad.UpMiddle: return(CreateRectSizableNode((quadRatio.RightUp.X + quadRatio.LeftUp.X) / 2, (quadRatio.RightUp.Y + quadRatio.LeftUp.Y) / 2)); default: return(new Rectangle()); } } }