How to calculate the clipping area of an image after scaling and translating
I have an image on a canvas and a ScaleTranform and a TranslateTransform attached to the image's RenderTranform. So with a bit of mouse event handling I can move and zoom the image within the 350 by 450 bounds of the canvas.
How would I calculate the clipping rectangle on the original BitmapImage, to that of the visible area on t开发者_如何转开发he screen, after some scaling and translation. I'd like to crop the original BitmapImage.
<Border BorderBrush="Black" BorderThickness="2">
<Canvas Name="canvas" ClipToBounds="True" Height="450" Width="350">
<Image Name="image" Opacity="1" RenderTransformOrigin="0.5,0.5" Height="450" Width="350">
<Image.Source>
<BitmapImage UriSource="test.jpg"/>
</Image.Source>
</Image>
</Canvas>
</Border>
Thanks
I would think it would be simple math to take the current ScaleTransform values and figure out what the actual size of the image is at the time, and then you know that you have a 350x450 box that you're going to crop out of that, you just need to use the current TranslateTransform to figure that out. Just keep in mind what you're using for the origins of these transforms, as that's what you need to calculate it from.
What I said above assumes that you have the ScaleTransform first in your RenderTransform and the TranslateTransform second. The order of operations does matter here.
Simply apply the calculations done to the Image by the Transformations to the bounds... (And do it in the same order...)
So if you Scale it up by a factor of 2 in X and Y the resulting Canvas will be 450/2 x 350/2 (in size - origin is still unknown)
And i guess it would be easier to avoid the TranslateTransform and simply play with the Origin of the ScaleTransform... whereas an Origin of 0 should give you an Clip-origin of 0 and a X&Y Origin of 1 will result in the image to be lower right...
so to set up some simple formula..:
double W = 450;
double H = 350;
double SX = 2;
double SY = 2;
double OX = .3;
double OY = .3;
double newW = W / SX;
double newH = H / SY;
double newX = (W-newW) * OX;
double newY = (H-newH) * OY;
so newX,Y,W and H contain the data you are looking for!
regards, dave
精彩评论