将图像的特定点放在另一个c#的特定点上。

问题描述 投票:-1回答:1

我有一张图片,坐标是 x1,y1. 让我们说 (300,500). 我想在那里放置一个50x50的图像,但不是该图像的左上角,而是 (x2,y2) 的小图像。(例如 (20,30))

就像这样 我希望这两个点在同一个地方enter image description here

我试过类似的东西,但这使我的小图像低于期望点。

  bigImage.DrawImage(smallImage, bigImage.x1, bigImage.y1,
    new Rectangle(smallImage.x2, smallImage.y2, smallImage.Height, smallImage.Width),
    GraphicsUnit.Pixel);

有什么办法吗?

编辑:刚刚意识到我的解决方案根本行不通,因为它裁剪了图像,而不是把它放在指定的点上。

c# drawimage
1个回答
1
投票

假设你有一个 image1 处于 (image1.x, image1.y). 而你有 image2 将被放置在 (image2.x, image2.y).

此外,还有一个目标位置在 image1. 该目标地点的坐标是 (image1.target.x, image1.target.y) 里面 image1. 让我们把这些坐标转换成原点... ...

内部 image1 我们在一个转换的坐标系上工作。它的偏移量是 (image1.x, image1.y). 我们需要加上这个偏移量。因此,目标的位置在 image1 相对于原点 (image1.x + image1.target.x, image1.y + image1.target.y).

同理,有一个目标位置在 image2. 该目标地点的坐标是 (image2.target.x, image2.target.y) 里面 image2. 这意味着目标的位置在 image2 相对于原点 (image2.x + image2.target.x, image2.y + image2.target.y).

我们希望这两个目标相对于原点在同一个地方。

(image1.x + image1.target.x, image1.y + image1.target.y)
=
(image2.x + image2.target.x, image2.y + image2.target.y)

让我按组件来分解

image1.x + image1.target.x = image2.x + image2.target.x
image1.y + image1.target.y = image2.y + image2.target.y

我们要通过改变原点的位置来存档 image2. 那就是通过改变 image2.ximage2.y. 让我们解决这些变量的方程。

image2.x = image1.x + image1.target.x - image2.target.x
image2.y = image1.y + image1.target.y - image2.target.y

这里是你必须放置第二张图片的地方.


希望一张图片能让你更清楚。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.