MeeraWeeks
MeeraWeeks

Reputation: 93

Placing specific point of image on specific point of another c#

I have a image with coordinates x1,y1. Lets say (300,500). I want to place there an image 50x50, but not top left corner of that image but (x2,y2) of the smaller image. (for example (20,30))

Something like this. I want the two dots to be in the same place enter image description here

I tried something like, but this puts my small image below desired point.

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

Is there a way to do it?

Edit: Just realized my solution doesn't work at all, because it crops the image instead of placing it on specified point.

Upvotes: 0

Views: 427

Answers (1)

Theraot
Theraot

Reputation: 40105

Let us say you have an image1 that is placed at (image1.x, image1.y). And you have image2 that will be placed at (image2.x, image2.y).

Furthermore, there is a target location in image1. This target location has coordinates (image1.target.x, image1.target.y) inside of image1. Let us transform those coordinates to the origin…

Inside image1 we work on a translated coordinate system. It is offset by (image1.x, image1.y). We need to add that offset. Thus, the position of the target in image1 relative to the origin is (image1.x + image1.target.x, image1.y + image1.target.y).

Similarly, there is a target location in image2. This target location has coordinates (image2.target.x, image2.target.y) inside of image2. Which means the position of the target in image2 relative to the origin is (image2.x + image2.target.x, image2.y + image2.target.y).

We want these two targets to be at the same place relative to the origin:

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

Let me break that by component:

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

We want to archive this by changing the position of image2. That is by changing the image2.x and image2.y. Let us solve the equations for those variables:

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

And there is where you must place the second image.


Hopefully a picture makes this clearer:

enter image description here

Upvotes: 1

Related Questions