I have a bitmap image where some points are located on it. Each point has a pair of coordinates (x, y) on the bitmap image (a jpeg file).
I need to find the bounding box of the points (NOT the image) so that I can specify a rectangle that cover the regions with the points such that
The region left bound = minimal x of all points
The region right bound = maximal x of all points
The region top bound = minimal y of all points
The region bottom bound = maximal y of all points
The solution here How to remove a border with an unknown width from an image does not work for me because it is for finding a rectangular region inside an image. I have some randomly located points on the image.
The solution here Getting the bounding box of a vector of points? does not work for me either because the coordinates of points have been transformed in th ebitmap image.
My C# code in VS2013:
System.Drawing.Bitmap canvasImage = new System.Drawing.Bitmap(12 * 64 - (2 * 4), 15 * 64 - ( 5 * 64), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(canvasImage);
var xr = Enumerable.Range( 2 , 12 );
var yr = Enumerable.Range( 5 , 15 );
foreach (int x in xr)
{
foreach (int y in yr)
{
int[] imageCornerXY = new int[4];
System.Drawing.Bitmap tempImage = … // a function generate an image
g.DrawImage(tempImage, new System.Drawing.PointF(x * 256 - (startX * 256), y * 256 - (startY * 256)));
}
}
// the image bounds have: top : 0 , bottom: 640, west: 0 east: 760
RectangleF imageBounds = canvasImage.GetBounds();
// Suppose that I have some points located in the region of (minCornerX, minCornerY), and (maxCornerX, maxCornerY), how to copy them to a new image and their relative locations are not changed ?
Rectangle rect = new Rectangle( minCornerX, minCornerY, maxCornerX - minCornerX , minCornerY - minCornerY);
// I need to copy the region covered by this rectangle to a new image.
Bitmap newMapImage = copyImage(canvasImage, rect);
My code just make a copy of an empty image except that I increase the size of rect to cover a large region. But, I just need a bounding box to cover the points exactly without any margins.
Any help would be appreciated.