给图片加热点是web开发中经常用到的一个功能。这方面的工具也不少。
为了更好的满足自己的需求,写了一个winform程序。
可以方便的给图片加热点,更方便灵活!
源码下载 http://download.csdn.net/download/qq_29939347/10150681
生成的代码:
1 2
技术要点
1 画图
热点图形有 矩形,多边形,圆形等。为了统一处理,定义了接口:
1 interface IDrwaShap 2 { 3 void Draw(System.Drawing.Graphics g); 4 string GetLink(); 5 bool Hit(int x, int y); 6 bool IsChecked(); 7 void SetChecked(bool sel); 8 void SetEndPoint(System.Drawing.Point end); 9 void SetLink(string link);10 11 void AddPoint(int x,int y);12 13 void SetStartMovePoint(int x, int y);14 void SetCurMovePoint(int x, int y);15 16 int GetShapId();17 18 ListGetPoint();19 20 }
矩形实现类为HotShapRect,多边形为HotShapPoly。
1 class HotShapPoly : PicHot.IDrwaShap 2 { 3 static Pen _drawPen = new Pen(Color.Red, 2); 4 static Pen _checkedPen = new Pen(Color.Blue, 2); 5 6 public int _shapId = 0; 7 private bool _checked = false; 8 private bool _startMove = false; 9 10 private Point _moveStartPoint = new Point(0, 0); 11 string _linkText = string.Empty; 12 13 List_listPoint = new List (); 14 public HotShapPoly() 15 { 16 _shapId = AppNum.GetId(); 17 } 18 public int GetShapId() 19 { 20 return _shapId; 21 } 22 23 public List GetPoint() 24 { 25 return _listPoint; 26 } 27 public void AddPoint(int x, int y) 28 { 29 Point newPt = new Point(x,y); 30 foreach (Point p in _listPoint) 31 { 32 if (p == newPt) 33 return; 34 } 35 36 _listPoint.Add(new Point(x, y)); 37 } 38 39 public void SetLink(string link) 40 { 41 _linkText = link; 42 } 43 public string GetLink() 44 { 45 return _linkText; 46 } 47 public void SetEndPoint(Point end) 48 { 49 AddPoint(end.X, end.Y); 50 } 51 52 public void SetChecked(bool sel) 53 { 54 _checked = sel; 55 _startMove = _checked; 56 } 57 58 public bool IsChecked() 59 { 60 return _checked; 61 } 62 63 public void SetStartMovePoint(int x, int y) 64 { 65 _moveStartPoint = new Point(x, y); 66 } 67 public void SetCurMovePoint(int x, int y) 68 { 69 if (!_startMove) 70 return; 71 72 Point moveEndPoint = new Point(x, y); 73 List _listPointNew = new List (); 74 foreach (Point p in _listPoint) 75 { 76 int m = p.X + (moveEndPoint.X - _moveStartPoint.X); 77 int n = p.Y + (moveEndPoint.Y - _moveStartPoint.Y); 78 _listPointNew.Add(new Point(m,n)); 79 } 80 _listPoint = _listPointNew; 81 82 _moveStartPoint = moveEndPoint; 83 } 84 85 86 public bool Hit(int x, int y) 87 { 88 if (_listPoint.Count <= 2) 89 return false; 90 91 Point hitPt = new Point(x,y); 92 93 GraphicsPath myGraphicsPath = new GraphicsPath(); 94 Region myRegion = new Region(); 95 myGraphicsPath.Reset(); 96 97 Point[] points = _listPoint.ToArray(); 98 myGraphicsPath.AddPolygon(points);//points); 99 myRegion.MakeEmpty();100 myRegion.Union(myGraphicsPath);101 //返回判断点是否在多边形里102 bool myPoint = myRegion.IsVisible(hitPt);103 104 return myPoint;105 }106 107 public void Draw(Graphics g)108 {109 if (_listPoint.Count <= 2)110 return ;111 112 Pen pen = _checked ? _checkedPen : _drawPen;113 114 g.DrawPolygon(pen, _listPoint.ToArray());115 }116 117 internal bool IsValidate()118 {119 if (_listPoint.Count <= 2)120 return false;121 return true;122 }123 124 }
2 生成代码
1 private void buttonCreateHtml_Click(object sender, EventArgs e) 2 { 3 StringBuilder sb = new StringBuilder(); 4 5 string mapName = string.Format("Map_{0}", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); 6 sb.AppendFormat("\r\n", fileName, mapName); 7 sb.AppendFormat("");14 Clipboard.SetText(sb.ToString());15 textBoxHtml.Text = sb.ToString();16 17 MessageBox.Show(this, "代码已复制到剪贴板!");18 }