使用 ImageSharp 放置旋转图像
Placing a rotated image with ImageSharp
我正在尝试旋转(围绕它的中心)并将一个图像放在另一个图像之上。
旋转后,我预期的 XY 坐标全错了。
非常感谢有关如何执行此操作的示例
当前正在绘制调试帧而不是图像。
我的坐标系基于展示位置的中心位置,但我可以将 XY 坐标切换到左上角
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)
);
}
}
}
(图片)Expected result (editor)
(图片)Result
我能够解决这个问题。
问题是客户端从未发送边界框的 XY 坐标。
相反,我尝试使用左上角的 XY 或中心位置 XY。
有了这个固定。我稍微调整了代码以反映这一变化。
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)
);
}
}
}
我正在尝试旋转(围绕它的中心)并将一个图像放在另一个图像之上。
旋转后,我预期的 XY 坐标全错了。
非常感谢有关如何执行此操作的示例
当前正在绘制调试帧而不是图像。
我的坐标系基于展示位置的中心位置,但我可以将 XY 坐标切换到左上角
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)
);
}
}
}
(图片)Expected result (editor)
(图片)Result
我能够解决这个问题。
问题是客户端从未发送边界框的 XY 坐标。 相反,我尝试使用左上角的 XY 或中心位置 XY。
有了这个固定。我稍微调整了代码以反映这一变化。
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)
);
}
}
}