コード例 #1
0
        /// <summary>
        /// Generate a bitmap showing an overhead view of the gps waypoints
        /// </summary>
        /// <param name="width"></param>
        /// <returns></returns>
        private Stream GenerateTop(int width)
        {
            MemoryStream memory = null;

            int height = width * 3 / 4;
            using (Bitmap bmp = new Bitmap(width, height))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.LightGray);
                    g.DrawRectangle(Pens.White, new Rectangle(-1, -1, 3, 3));

                    if (_state.History == null || _state.History.Count < 1)
                    {
                        g.DrawString("No Data - check 'Capture History' box to see the track", new Font(FontFamily.GenericSansSerif, 16, GraphicsUnit.Pixel), Brushes.Red, new Point(10, 10));
                    }
                    else // plot a simple map from the Gps waypoints
                    {

                        double minLongitude = 9999.0, minLatitude = 9999.0, minAltitude = 9999999.0;
                        double maxLongitude = -9999.0, maxLatitude = -9999.0, maxAltitude = -9999999.0;

                        foreach (EarthCoordinates ec in _state.History)
                        {
                            minLongitude = Math.Min(minLongitude, ec.Longitude);
                            minLatitude = Math.Min(minLatitude, ec.Latitude);
                            minAltitude = Math.Min(minAltitude, ec.AltitudeMeters);
                            maxLongitude = Math.Max(maxLongitude, ec.Longitude);
                            maxLatitude = Math.Max(maxLatitude, ec.Latitude);
                            maxAltitude = Math.Max(maxAltitude, ec.AltitudeMeters);
                        }

                        if (minLongitude < 0)
                        {
                            double hold = minLongitude;
                            minLongitude = maxLongitude;
                            maxLongitude = hold;
                        }

                        EarthCoordinates start = new EarthCoordinates(minLatitude, minLongitude, minAltitude);
                        EarthCoordinates end = new EarthCoordinates(maxLatitude, maxLongitude, maxAltitude);
                        Point3 box = end.OffsetFromStart(start);
                        double scale = Math.Max(box.X / (double)width, box.Y / (double)height);

                        Point lastPoint = Point.Empty;
                        Point currentPoint;
                        foreach (EarthCoordinates ec in _state.History)
                        {
                            box = ec.OffsetFromStart(start);
                            currentPoint = new Point(width - (int)(box.Y / scale) - 10, height - (int)(box.X / scale) - 10);

                            // Draw the point.
                            int dop = (int)Math.Truncate(ec.HorizontalDilutionOfPrecision);
                            Rectangle r = new Rectangle(currentPoint.X - dop, currentPoint.Y - dop, dop * 2, dop * 2);
                            g.FillEllipse(Brushes.Azure, r);
                            g.DrawEllipse(Pens.Yellow, r);

                            if (lastPoint == Point.Empty)
                                g.DrawString("Start", new Font(FontFamily.GenericSansSerif, 16, GraphicsUnit.Pixel), Brushes.Green, currentPoint);
                            else
                            {
                                g.DrawLine(Pens.Red, lastPoint, currentPoint);
                            }
                            lastPoint = currentPoint;
                        }
                    }
                }

                memory = new MemoryStream();
                bmp.Save(memory, ImageFormat.Jpeg);
                memory.Position = 0;

            }
            return memory;
        }