开发者

WPF image cropping

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 < to开发者_开发问答talImages; i++)
        {
            Image image = new Image();
            image.Source = Window1.GetNextImage(i);

            canvas1.Children.Add(image);                
        }


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);


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜