對我來說幸運的矩形都相同的大小,所以我可以很容易地找到爲5.0 ScaleTrensformation這樣將每個矩形融入一個固定刻度值查看端口。一旦確定,我可以拿出以下函數來根據圖像中的座標計算TranslateTransform的值。希望它可以幫助處於類似情況的人們。
public void SetImageCoordinate(double x, double y)
{
TransformGroup transformGroup = (TransformGroup)image.RenderTransform;
ScaleTransform transform = (ScaleTransform)transformGroup.Children[0];
ImageSource imageSource = image.Source;
BitmapImage bitmapImage = (BitmapImage) imageSource ;
//Now since you got the image displayed in Image control. You can easily map the mouse position to the Pixel scale.
var pixelMousePositionX = -(x)/bitmapImage.PixelWidth * transform.ScaleX * image.ActualWidth;
var pixelMousePositionY = -(y)/bitmapImage.PixelHeight * transform.ScaleY * image.ActualHeight;
//MessageBox.Show("X: " + pixelMousePositionX + "; Y: " + pixelMousePositionY);
var tt = (TranslateTransform)((TransformGroup)image.RenderTransform).Children.First(tr => tr is TranslateTransform);
tt.X = pixelMousePositionX;
tt.Y = pixelMousePositionY;
}
感謝您的回答。但是,那麼我不必扔掉縮放和平移功能?我想保留巨大的文件,以便用戶可以通過縮放和平移移動到其他部分。 –
@Thomas檢查這個,也許幫助你:http://www.codeproject.com/Articles/20245/WPF-Interactive-Image-Cropping-Control或http://stackoverflow.com/a/4551514/440030 –