Exemplo n.º 1
0
        /// <summary>
        /// Given a 2D array of segmented bitmaps, this method will stitch all of them together into one super bitmap.
        /// </summary>
        /// <param name="bitmapCollection">2D Array of segmented bitmaps</param>
        public void StitchedBitmap(Bitmap[,] bitmapCollection)
        {
            // width and height can be calculated ahead of time @bitmapCollection creation
            int totalWidth  = 0;
            int totalHeight = 0;

            // GetUpperBound() method is very slow. Call this once outside of loop to save time.
            int bitmapCollectionI = bitmapCollection.GetUpperBound(0);
            int bitmapCollectionJ = bitmapCollection.GetUpperBound(1);

            // loop through collection to find width and height of newly created bitmap
            for (int i = 0; i <= bitmapCollectionI; i++)
            {
                for (int j = 0; j <= bitmapCollectionJ; j++)
                {
                    try
                    {
                        // counting width (i)
                        if (i == 0)
                        {
                            totalWidth += bitmapCollection[i, j].Width;
                        }
                        // counting height (j)
                        if (j == 0)
                        {
                            totalHeight += bitmapCollection[i, j].Height;
                        }
                    }
                    catch (NullReferenceException nullRefrenceException)
                    {
                        Console.WriteLine("Null refrence in bitmap stitching method");
                        Console.WriteLine(nullRefrenceException.ToString());
                    }
                    catch (Exception e)  // for generic exceptions
                    {
                        Console.WriteLine("Generic exception");
                        Console.WriteLine(e.ToString());
                    }
                }
            }
            Debug.WriteLine("BitmapStitchedSize: W:" + totalWidth + " H:" + totalHeight);
            Bitmap superBitmap = new Bitmap(totalWidth, totalHeight);
            int    currentY    = 0; // keeps track of y coordinate
            int    iterates    = 0;

            // loop for copying micro bitmaps onto super bitmap
            // @SOURCE: https://stackoverflow.com/questions/9616617/c-sharp-copy-paste-an-image-region-into-another-image
            using (Graphics g = Graphics.FromImage(superBitmap))
            {
                //g.FillRectangle(new SolidBrush(Color.White), 0, 0, totalWidth, totalHeight);
                // copy bitmap collection onto super bitmap
                for (int i = 0; i <= bitmapCollectionI; i++)
                {
                    int currentX = 0;   // keeps track of x coordinate
                    for (int j = 0; j <= bitmapCollectionJ; j++)
                    {
                        Bitmap sampledBitmap = bitmapCollection[i, j];
                        g.DrawImage(sampledBitmap, currentX, currentY);
                        currentX += sampledBitmap.Width;
                        if (j == bitmapCollectionJ)
                        {
                            currentY += sampledBitmap.Height;
                            Console.WriteLine(currentY);
                        }
                    }
                    currentX = 0;
                }
            }

            GMapOverlay mar = new GMapOverlay("m");
            GMapMarker  m   = new GMarkerGoogle(new PointLatLng(48.8617774, 2.349272), new Bitmap(@"C:\Users\Dev\Desktop\gtr.jpg"));

            mar.Markers.Add(m);
            gmap.Overlays.Add(mar);

            // casting superbitmap to a image for saving on disk
            Image superImage = (Image)superBitmap;

            try
            {
                superImage.Save(@"C:\Users\Dev\Desktop\432.png");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }