/// <summary> /// Computes the new image size. /// </summary> protected override Size CalculateNewImageSize(UnmanagedImage sourceData) { // Calculate source size float w = sourceData.Width; float h = sourceData.Height; // Get the four corners and the center of the image PointF[] corners = { new PointF(0, 0), new PointF(w, 0), new PointF(0, h), new PointF(w, h), new PointF(w / 2f, h / 2f) }; // Project those points corners = homography.Inverse().TransformPoints(corners); // Recalculate image size float[] px = { corners[0].X, corners[1].X, corners[2].X, corners[3].X }; float[] py = { corners[0].Y, corners[1].Y, corners[2].Y, corners[3].Y }; float maxX = Matrix.Max(px); float minX = Matrix.Min(px); float newWidth = Math.Max(maxX, overlayImage.Width) - Math.Min(0, minX); float maxY = Accord.Math.Matrix.Max(py); float minY = Accord.Math.Matrix.Min(py); float newHeight = Math.Max(maxY, overlayImage.Height) - Math.Min(0, minY); // Store overlay image size this.imageSize = new Size((int)Math.Round(maxX - minX), (int)Math.Round(maxY - minY)); // Store image center this.center = Point.Round(corners[4]); // Calculate and store image offset int offsetX = 0, offsetY = 0; if (minX < 0) { offsetX = (int)Math.Round(minX); } if (minY < 0) { offsetY = (int)Math.Round(minY); } this.offset = new Point(offsetX, offsetY); // Return the final image size return(new Size((int)Math.Ceiling(newWidth), (int)Math.Ceiling(newHeight))); }