예제 #1
0
        private Bitmap StripQuickRouteHeader(Stream stream)
        {
            Bitmap targetImage = null;

            if (stream != null)
            {
                // check if the image is an exported QuickRoute image; if yes, remove the border
                var ed          = QuickRouteJpegExtensionData.FromStream(stream);
                var sourceImage = System.Drawing.Image.FromStream(stream);
                rawData         = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(rawData, 0, (int)stream.Length);
                if (ed != null)
                {
                    targetImage = new Bitmap(ed.MapLocationAndSizeInPixels.Width, ed.MapLocationAndSizeInPixels.Height);
                    var g = Graphics.FromImage(targetImage);
                    g.DrawImage(sourceImage, new Rectangle(new Point(0, 0), ed.MapLocationAndSizeInPixels.Size), ed.MapLocationAndSizeInPixels, GraphicsUnit.Pixel);
                    g.Dispose();
                }
                else
                {
                    targetImage = new Bitmap(sourceImage);
                }
                stream.Close();
                stream.Dispose();
            }
            return(targetImage);
        }
예제 #2
0
        private void CalculateImageAndTransformationMatrix(string fileName)
        {
            var zipFile = new Ionic.Zip.ZipFile(fileName);
            var mapSize = new Size();

            Transformation = new Transformation();

            // get entry for kml file and image file
            KmlDocument kmlDocument = null;

            foreach (var entry in zipFile)
            {
                if (entry.FileName == entry.LocalFileName && Path.GetExtension(entry.FileName) == ".kml")
                {
                    using (var kmlStream = new MemoryStream())
                    {
                        entry.Extract(kmlStream);
                        kmlStream.Position = 0;
                        kmlDocument        = new KmlDocument(kmlStream);
                    }
                    break;
                }
            }

            if (kmlDocument != null)
            {
                // we have got a kml document, get map image file stream from it
                foreach (var entry in zipFile)
                {
                    if (entry.FileName == kmlDocument.ImageFileName)
                    {
                        ImageStream = new MemoryStream();
                        entry.Extract(ImageStream);
                        ImageStream.Position = 0;
                        // check if image is QR jpeg
                        var ed = QuickRouteJpegExtensionData.FromStream(ImageStream);
                        if (ed != null)
                        {
                            // get transformation matrix from QR jpeg metadata
                            Transformation       = ed.Sessions.CalculateAverageTransformation();
                            ImageStream.Position = 0;
                            return;
                        }
                        else
                        {
                            // it is not, use normal image bounds
                            ImageStream.Position = 0;
                            mapSize = Image.FromStream(ImageStream).Size; // need to get image object to get image size
                        }
                        ImageStream.Position = 0;
                        break;
                    }
                }
            }

            if (kmlDocument != null && ImageStream != null)
            {
                // finally, calculate the transformation
                Transformation = new Transformation(kmlDocument.LongLatBox, mapSize);
            }
        }