tags:

views:

239

answers:

3

I'm loading layers of images to make a single image. I'm currently stacking them all onto a canvas. I've got it set up so the user can specify the final dimensions of the single image, but even when I change the size of the canvas the images retain their original sizes.

I tried to modify the image size as I was loading them in, but the sizes were NaN and the Actual Sizes were 0 so I couldn't change them there.

I'm starting to think that canvas might not be the way to go. Any suggestions to how I could clip images to fit a specific size?

        canvas1.Children.Clear();
        int totalImages = Window1.GetNumberOfImages();
        if (drawBackground)
            canvas1.Background = new SolidColorBrush(Color.FromArgb(a,r,g,b));
        else
            canvas1.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

        for (int i = 0; i < totalImages; i++)
        {
            Image image = new Image();
            image.Source = Window1.GetNextImage(i);

            canvas1.Children.Add(image);                
        }
A: 

Seems to me like you should be modifying the size of the image sources, not just the canvas. Canvas is just a container, it doesn't have the capability to modify its children.

Scott J
Right, that's what I'm trying to do. However when I load them in the sizes were set to NaN and 0.
Califer
What's the type of the object being returned from Window1.GetNextImage(i)? That's the one with NaN and 0?
Scott J
It's a BitmapSource. I'm sure that it has the right information for the size, but when I load it into the Image the Image doesn't have the correct sizes.
Califer
+1  A: 

To get the current sizes of the canvas, you must first call Measure and Arrange. This will avoid the NaNs and 0s.

Use RenderTransform to change the size of the image that the Canvas displays.

I have never tried to crop an image, so I don't know how to do that, but I see that there is a CroppedBitmap object. I guess you tried that already?

Jeffrey L Whitledge
Haven't tried that yet, didn't even know about it! I'll give that a try once I can get back to my own computer.
Califer
I changed the inside of the for loop to the following and it works now.Image image = new Image();BitmapSource tempSource = Window1.GetNextImage(i);CroppedBitmap cb = new CroppedBitmap(tempSource, new Int32Rect(0, 0, Math.Min((int)Window1.totalWinWidth, tempSource.PixelWidth), Math.Min((int)Window1.totalWinHeight, tempSource.PixelHeight)));image.Source = cb;canvas1.Children.Add(image);
Califer
>_< I guess I can't put code formatting in a comment...
Califer
A: 

For anyone else who's doing the same thing here's the code that I got working. Thanks Jeffrey!

            Image image = new Image();
            BitmapSource tempSource = Window1.GetNextImage(i);
            CroppedBitmap cb = new CroppedBitmap(tempSource,
                        new Int32Rect(0, 0, 
                            Math.Min((int)Window1.totalWinWidth, tempSource.PixelWidth), 
                            Math.Min((int)Window1.totalWinHeight, tempSource.PixelHeight)));
            image.Source = cb;

            canvas1.Children.Add(image);
Califer
You're welcome! Glad I could help. :-)
Jeffrey L Whitledge