0

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.

Community
  • 1
  • 1
user2754279
  • 115
  • 1
  • 3
  • 10
  • Are you talking about visually changing the image in the UI, or changing the actual image file? – Sheridan Jan 23 '14 at 16:11
  • Hi, no I'm just want to Change the image in the UI. The actual image shouldn't be changed. Is there any simple code to do that? – user2754279 Jan 23 '14 at 16:14

1 Answers1

1

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.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • How does it allow me to crop the Image by the given distance (top by 18 Pixels, bottom by 36 Pixels, left by 48 Pixels, and right by 24 pixels)? After the crop, I think I can use 'Stretch' to resize it. – user2754279 Jan 23 '14 at 16:32
  • That's a good point... I'll look into it, or think of something else. – Sheridan Jan 23 '14 at 16:52
  • You can set the `Margin` to negative values to crop the `Image`... see my edited example. – Sheridan Jan 23 '14 at 16:55