mpen
mpen

Reputation: 282805

How to crop an image with a rotated rectangle?

I have an image in the form of a System.Drawing.Bitmap and a rectangle in the form of 4 points (Vector2s which are trivially converted to PointFs).

I want to use those points to crop out a section of the image. I found this answer which is pretty close to what I want, but I'm not sure how to get the right matrix out of it.

Here's what I've got so far:

protected static Bitmap CropImage(Bitmap src, Vector2[] rect)
{
    var width = (rect[1] - rect[0]).Length;
    var height = (rect[3] - rect[0]).Length;
    var result = new Bitmap(M2.Round(width), M2.Round(height));
    using (Graphics g = Graphics.FromImage(result))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        using (Matrix mat = new Matrix())
        {
             // ????
        }
    }
    return result;
}

How can I get the proper transform matrix out of my rect?

Upvotes: 7

Views: 2639

Answers (2)

mpen
mpen

Reputation: 282805

Figured it out:

protected static Bitmap CropImage(Bitmap src, Vector2[] rect)
{
    var width = (rect[1] - rect[0]).Length;
    var height = (rect[3] - rect[0]).Length;
    var result = new Bitmap(M2.Round(width), M2.Round(height));
    using (Graphics g = Graphics.FromImage(result))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        using (Matrix mat = new Matrix())
        {
            var rot = -Math.Atan2(rect[1].Y - rect[0].Y, rect[1].X - rect[0].X) * M2.RadToDeg;

            mat.Translate(-rect[0].X, -rect[0].Y);
            mat.RotateAt((float)rot, rect[0].ToPointF());

            g.Transform = mat;
            g.DrawImage(src, new Rectangle(0, 0, src.Width, src.Height));
        }
    }
    return result;
}

Upvotes: 1

Ry-
Ry-

Reputation: 224859

It would be the same as in the linked answer, but instead of:

mat.Translate(-rect.Location.X, -rect.Location.Y);
mat.RotateAt(angle, rect.Location);

You would use:

double angle = Math.Atan2(rect[1].Y - rect[0].Y, rect[1].X - rect[0].X);
mat.Translate(-rect[0].X, -rect[0].Y);
mat.RotateAt((float)angle, rect[0]);

(Or something along those lines. It may be -angle, or rect[0] instead of rect[1] and vice-versa in Atan2. I can’t check immediately…)

Upvotes: 1

Related Questions