For my TruckWars game, I needed to get the profile shot for an image. For example, when you select an object, it displays an image of that object in the dashboard. The two problems are:
What if the image is bigger than the dashboard's profile size? (i..e image is 96x96, but the dashboard only allows 48x48).
What if the image has multiple frames for animation (i.e. image displays as 48x48, but has two frames, and therefore actual size is 96x48).
I wanted a way that given any image, it would clip the image to a single frame, and then shrink it to fit within the allowed-profile size. You can do this if you know the actual image size. In this case, the size of the profile image in the dashboard is 48 pixels. Because I make the animation frames be horizontal (i.e. something with two frames is twice as wide), I scale the image based on height. Then I clip to just the first frame.
public static void MakeProfileImage(ref Image image1, Size szActual)
{
//Scale back the img to fit to the view size
const double profileHeight = 48;
double dblScale = szActual.Height / profileHeight;
image1.Width = szActual.Width / dblScale;
image1.Height = profileHeight;
//Always clip the view size (in case there were multiple frames for animation)
RectangleGeometry r = new RectangleGeometry();
r.Rect = new Rect(0, 0, profileHeight, profileHeight);
image1.Clip = r;
}
I added this to a ImageUtilities class for reuse later.
No comments:
Post a Comment