Question

I Need to crop and resize an Image in my WPF application as soon as the Picture is loaded. So, my Basic Image has a VGA size (640x480), and I Need to crop the edges (top by 18 Pixels, bottom by 36 Pixels, left by 48 Pixels, and right by 24 pixels). The new image (which is 568 x 426 pixels) Need to be refitted into the original size (640 x 480 pixels) - basically it's like a digital zoomc that we're using in photography.

I've already found some sample code (Cropping whitespace from image in C#) - this is however a Little bit too complicated since I don't Need to detect the Whitespace on the image. Is there any simple algorithm just by using XAML to do this ?

Thanks in advance.

Was it helpful?

Solution

I think that you should be able to do that by using the Viewbox Class. From the linked page: Defines a content decorator that can stretch and scale a single child to fill the available space. You literally add one to your Window and set your Image as the contents and then you can set properties to control which part of the image it displays:

<ViewBox Width="500" Height="500" Stretch="Uniform">
    <Image Source="Images/SomeImage.jpg" Width="300" Height="300" 
        Margin="-48,-18,-36,-24" />
</ViewBox>

Experiment with the different StretchDirection values and set the Margin to negative values to crop. There are examples in the linked page, but let me know if you need more help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top