zandzpider
zandzpider

Reputation: 11

Placing a rotated image with ImageSharp

I'm trying to rotate (around it's center) and place an image on top of another image.

After rotation, the XY coordinates i expected it to be in, is all wrong.

An example on how to do this would be greatly appreciated

Currently drawing debug frames instead of an image.

My coordinate system is based on a center position of the placement, but i could switch to a Top Left one for the XY coordinates

private static void DrawDebugFrames(List<LogoPlacementContentDto> placements, Image<Rgba32> mutatedImage)
{
    foreach (var placement in placements)
    {
        var width = placement.Width;
        var height = placement.Height;

        using (var logo = new Image<Rgba32>(Configuration.Default, width, height))
        {
            var centerX = placement.X; // center of imagePlacement
            var centerY = placement.Y; // center of imagePlacement

            var affineBuilder = new AffineTransformBuilder();
            affineBuilder.PrependTranslation(new Vector2(centerX, centerY));
            affineBuilder.PrependRotationDegrees(placement.Rotation);

            logo.Mutate(
                x => x
                    .BackgroundColor(Rgba32.Beige).DrawPolygon(
                        Rgba32.HotPink,
                        4,
                        new Vector2(0, 0),
                        new Vector2(width, 0),
                        new Vector2(width, height),
                        new Vector2(0, height)
                    )
                    .Transform(affineBuilder)
            );

            mutatedImage.Mutate(
                x => x
                    .DrawImage(logo, new Point(-(width / 2), -(height / 2)), GraphicsOptions.Default)
            );
        }
    }
}

(Image) Expected result (editor)

(Image) Result

Upvotes: 0

Views: 1505

Answers (1)

zandzpider
zandzpider

Reputation: 11

I was able to solve this.

The issue was that the client never sent the XY coordinate of the bounding box. Instead i tried to use the XY of the top left corner or the center position XY.

With this fixed. i adjusted the code slightly to reflect this change.

private static void DrawDebugFrames(List<LogoPlacementContentDto> placements, Image<Rgba32> mutatedImage)
{
    foreach (var placement in placements)
    {
        var width = placement.WidthInt;
        var height = placement.HeightInt;

        using (var logo = new Image<Rgba32>(Configuration.Default, width, height))
        {
            var positionX = placement.Position.X;
            var positionY = placement.Position.Y;

            var affineBuilder = new AffineTransformBuilder();

            affineBuilder.PrependTranslation(new Vector2(positionX, positionY));
            affineBuilder.PrependRotationDegrees(placement.Rotation);
            affineBuilder.AppendTranslation(new Vector2(-positionX, -positionY));

            logo.Mutate(
                x => x
                    .BackgroundColor(Rgba32.Beige).DrawPolygon(
                        Rgba32.HotPink,
                        4,
                    new Vector2(0, 0),
                    new Vector2(width, 0),
                    new Vector2(width, height),
                    new Vector2(0, height)
                    )
                    .Transform(affineBuilder)
            );

            mutatedImage.Mutate(
                x => x
                    .DrawImage(logo, new Point(placement.Position.XInt, placement.Position.YInt), GraphicsOptions.Default)
            );
        }
    }
}

Upvotes: 1

Related Questions