예제 #1
0
        private void CreateMapAndSetInitialTransformations()
        {
            switch (SelectedImageFileFormat)
            {
            case ImageFileFormat.ImageFile:
                using (var ms = new MemoryStream(originalMapBytes))
                {
                    Map         = new Map(ms);
                    ms.Position = 0;
                    var ed = QuickRouteJpegExtensionData.FromStream(ms);
                    // is it a QuickRoute image? if yes, use embedded transformation matrix
                    if (ed != null && ed.Sessions != null && ed.Sessions.Count > 0)
                    {
                        InitialTransformation = ed.Sessions.CalculateAverageTransformation();
                    }
                    break;
                }

            case ImageFileFormat.QuickRouteFile:
                using (var ms = new MemoryStream(originalMapBytes))
                {
                    var d = Document.OpenFromQrt(ms);
                    if (d != null)
                    {
                        Map = d.Map;
                        InitialTransformation = d.Sessions.CalculateAverageTransformation();
                    }
                }
                break;

            case ImageFileFormat.KmzFile:
                using (var ms = new MemoryStream(originalMapBytes))
                {
                    var kmz = new KmzDocument(ms);
                    if (kmz.ImageStream != null)
                    {
                        Map = new Map(kmz.ImageStream);
                        InitialTransformation = kmz.Transformation;
                    }
                    break;
                }

            case ImageFileFormat.PdfFile:
                using (var ms = new MemoryStream(originalMapBytes))
                {
                    var pdfDocument = PdfDocument.Load(ms);
                    var image       = pdfDocument.Render(0, DPI_F, DPI_F, PdfRenderFlags.CorrectFromDpi);
                    Map = new Map(new Bitmap(image));
                    break;
                }
            }
        }
예제 #2
0
        private static Image GetImageFromBytes(byte[] bytes, ImageFileFormat fileFormat)
        {
            try
            {
                switch (fileFormat)
                {
                case ImageFileFormat.ImageFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        return(Image.FromStream(ms));
                    }

                case ImageFileFormat.PdfFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        var pdfDocument = PdfDocument.Load(ms);
                        return(pdfDocument.Render(0, DPI_F, DPI_F, PdfRenderFlags.CorrectFromDpi));
                    }

                case ImageFileFormat.QuickRouteFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        var d = Document.OpenFromQrt(ms);
                        return(d.Map.Image);
                    }

                case ImageFileFormat.KmzFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        var kmz = new KmzDocument(ms);
                        if (kmz.ImageStream != null)
                        {
                            return(Image.FromStream(kmz.ImageStream));
                        }
                    }
                    break;
                }
            }
            catch
            {
            }

            return(null);
        }
예제 #3
0
        private static Image GetImageFromBytes(byte[] bytes, ImageFileFormat fileFormat)
        {
            try
            {
                switch (fileFormat)
                {
                case ImageFileFormat.ImageFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        return(Image.FromStream(ms));
                    }

                case ImageFileFormat.QuickRouteFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        var d = Document.OpenFromQrt(ms);
                        return(d.Map.Image);
                    }

                case ImageFileFormat.KmzFile:
                    using (var ms = new MemoryStream(bytes))
                    {
                        var kmz = new KmzDocument(ms);
                        if (kmz.ImageStream != null)
                        {
                            return(Image.FromStream(kmz.ImageStream));
                        }
                    }
                    break;
                }
            }
            catch
            {
            }

            return(null);
        }
예제 #4
0
        private DialogResult Import()
        {
            // validate file names
            if (mapImageFromFile.Checked && !File.Exists(mapImageFileName.Text))
            {
                MessageBox.Show(Strings.MapImageFileDoesNotExist, Strings.InvalidMapImage, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(DialogResult.Cancel);
            }
            if (routeFromFile.Checked && !File.Exists(routeFileName.Text))
            {
                MessageBox.Show(Strings.RouteFileDoesNotExist, Strings.InvalidRoute, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(DialogResult.Cancel);
            }

            IRouteImporter routeImporter = null;

            if (routeFromFile.Checked)
            {
                IRouteFileImporter routeFileImporter = ((RouteFileFormat)routeFileFormatComboBox.SelectedItem).Importer;
                routeFileImporter.FileName = routeFileName.Text;
                routeImporter = routeFileImporter;
            }
            else if (routeFromGpsDevice.Checked)
            {
                GPSDevice gpsDevice = routeGpsDevice.SelectedItem as GPSDevice;
                if (gpsDevice == null)
                {
                    MessageBox.Show(Strings.NoGPSDevicesConnectedMessageBox, Strings.InvalidRoute, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return(DialogResult.Cancel);
                }
                routeImporter = gpsDevice.Importer;
            }

            routeImporter.BeginWork    += routeImporter_BeginWork;
            routeImporter.WorkProgress += routeImporter_WorkProgress;
            routeImporter.EndWork      += routeImporter_EndWork;

            DialogResult result;

            try
            {
                result = routeImporter.ShowPreImportDialogs();
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                Util.ShowExceptionMessageBox(ex, Strings.InvalidRoute);
                return(DialogResult.Cancel);
            }
            if (result == DialogResult.OK)
            {
                try
                {
                    routeImporter.Import();
                }
                catch (Exception ex)
                {
                    routeImporter.ImportResult.Succeeded    = false;
                    routeImporter.ImportResult.Error        = ImportError.Unknown;
                    routeImporter.ImportResult.ErrorMessage = ex.Message;
                    routeImporter.ImportResult.Exception    = ex;
                }

                if (!routeImporter.ImportResult.Succeeded)
                {
                    // an error occured, show relevant error info and cancel creation of new document.
                    switch (routeImporter.ImportResult.Error)
                    {
                    case ImportError.NoWaypoints:
                        routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypoints;
                        break;

                    case ImportError.NoWaypointTimes:
                        routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypointTimes;
                        break;
                    }
                    Cursor = Cursors.Default;
                    if (routeImporter.ImportResult.Exception != null)
                    {
                        Util.ShowExceptionMessageBox(routeImporter.ImportResult.Exception, Strings.InvalidRoute);
                    }
                    else
                    {
                        MessageBox.Show(routeImporter.ImportResult.ErrorMessage, Strings.InvalidRoute, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    return(DialogResult.Cancel);
                }

                try
                {
                    if (mapImageFromFile.Checked)
                    {
                        switch (mapImageFileFormatComboBox.SelectedIndex)
                        {
                        case 0: // map image from image file
                            Map = new Map(mapImageFileName.Text, MapSourceType.FileSystem, MapStorageType.Inline);
                            // is it a QuickRoute image? if yes, use embedded transformation matrix
                            var ed = QuickRouteJpegExtensionData.FromJpegFile(mapImageFileName.Text);
                            if (ed != null && ed.Sessions != null && ed.Sessions.Count > 0)
                            {
                                InitialTransformation = ed.Sessions.CalculateAverageTransformation();
                            }
                            break;

                        case 1: // map image from QuickRoute file
                            Document d = Document.OpenFromQrt(mapImageFileName.Text);
                            if (d != null)
                            {
                                Map = d.Map;
                                InitialTransformation = d.Sessions.CalculateAverageTransformation();
                            }
                            break;

                        case 2: // map image from kmz file
                            var kmz = new KmzDocument(mapImageFileName.Text);
                            if (kmz.ImageStream != null)
                            {
                                Map = new Map(kmz.ImageStream);
                                InitialTransformation = kmz.Transformation;
                            }
                            break;
                        }
                    }
                    else if (mapImageBlank.Checked)
                    {
                        var blankMap = new Bitmap(1500, 1500);
                        using (Graphics g = Graphics.FromImage(blankMap))
                        {
                            g.Clear(Color.White);
                        }
                        Map = new Map(blankMap);
                    }
                    else if (mapImageFromUrl.Checked)
                    {
                        Map = new Map(mapImageUrl.Text, MapSourceType.Url, MapStorageType.Inline);
                    }
                }
                catch (Exception ex)
                {
                    Cursor = Cursors.Default;
                    Util.ShowExceptionMessageBox(ex, Strings.InvalidMapImage);
                    return(DialogResult.Cancel);
                }
                ImportResult = routeImporter.ImportResult;

                if (mapImageFromFile.Checked)
                {
                    Util.ApplicationSettings.AddRecentMapImageFileName(mapImageFileName.Text);
                }
                if (routeFromFile.Checked)
                {
                    Util.ApplicationSettings.AddRecentRouteFileName(routeFileName.Text);
                }
            }
            return(result);
        }