//魔术棒判识 private void HandleMagicWand(object result, Dictionary <string, object> args) { GeometryOfDrawed geometry = result as GeometryOfDrawed; IRasterDrawing drawing = GetRasterDrawing(_canvasViewer); if (drawing == null) { return; } double prjX = 0, prjY = 0; ICoordinateTransform tran = _canvasViewer.Canvas.CoordTransform; //如果鼠标没有点中图像有效区域则直接退出 tran.Raster2Prj(geometry.RasterPoints[0].X, geometry.RasterPoints[0].Y, out prjX, out prjY); if (prjX < drawing.OriginalEnvelope.MinX || prjX > drawing.OriginalEnvelope.MaxX || prjY < drawing.OriginalEnvelope.MinY || prjY > drawing.OriginalEnvelope.MaxY) { return; } //计算鼠标在当前视窗位图中的位置 int x = (int)((prjX - drawing.Envelope.MinX) / (drawing.Envelope.Width / (float)drawing.Bitmap.Width)); int y = (int)((drawing.Envelope.MaxY - prjY) / (drawing.Envelope.Height / (float)drawing.Bitmap.Height)); using (IBitmapMagicWand magicWand = new BitmapMagicWand()) { Bitmap crtBitmap = drawing.Bitmap; BitmapData pdata = crtBitmap.LockBits(new Rectangle(0, 0, crtBitmap.Width, crtBitmap.Height), ImageLockMode.ReadWrite, crtBitmap.PixelFormat); try { byte tolerance = 32; bool isContinued = true; if (args != null && args.Count == 2) { tolerance = byte.Parse(args["tolerance"].ToString()); isContinued = bool.Parse(args["iscontinued"].ToString()); } /* * 魔术棒提取 * ScanLineSegment为像素坐标扫描线三元组(Row,BeginCol,EndCol) */ ScanLineSegment[] segs = magicWand.ExtractSnappedPixels(pdata, new Point(x, y), tolerance, isContinued); /* * 将扫描线数组转换为判识结果 */ MagicWandResult2ExtractReulst(segs, drawing); } finally { crtBitmap.UnlockBits(pdata); } _canvasViewer.Canvas.Refresh(enumRefreshType.All); } }
private unsafe void Project(ContourLine cntLine, ICanvas canvas) { ICoordinateTransform tran = canvas.CoordTransform; if (tran == null) { return; } int nCount = cntLine.Count; fixed(PointF *ptr0 = cntLine.Points) { PointF *ptr = ptr0; for (int i = 0; i < nCount; i++, ptr++) { tran.Raster2Prj(ptr); } } cntLine.UpdateEnvelope(); }