简体   繁体   中英

Pixel location (x,y coordinate) from the image when image is resized

I want the pixel location(x,y co-ordinate) from the image actual scenario is

1) when fit the image into picture box it gives me an correct x,y pixel co-ordinate using mouse pointer it gives me correct pixel co-ordinate.

but

2) when i re-size the image and get the pixel co-ordinate it of that image it shows me wrong pixel co-ordinate.

while search on google some of refer me to use Matrix class of Microsoft .net but didn't know how to use it to get location the pixel location(x,y co-ordinate) while the image is resize?

if possible give me source of reference.....

Using a matrix is overkill. Given an image's original size and new resized dimensions, you should be able to do some simple math to translate a pixel in the resized image to a region in the original image:

// Given origWidth, origHeight, newWidth, newHeight, clickX, and clickY variables...
var scaleX = (double)origWidth / newWidth;
var scaleY = (double)origHeight / newHeight;
var translatedRegion = new {
    X = clickX * scaleX,
    Y = clickY * scaleY,
    Width = scaleX,
    Height = scaleY
};

Note that I'm not translating to a pixel, but to a region, because one pixel may not translate to just a single or even a whole pixel in the original.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM