5

Give an image:

Image tileSet = new Image();
tileSet.Source = new BitmapImage(new Uri(@".."));

How can i cropt it, defining a rectangle area?

J.Silver
  • 75
  • 2
  • 4
  • 1
    Possible duplicate of [Image manipulating with WPF](http://stackoverflow.com/questions/438769/image-manipulating-with-wpf) – Al.G. Oct 18 '15 at 17:59

2 Answers2

12

You can use CroppedBitmap for that

var fullBitmap = new BitmapImage(new Uri(@".."));
tileSet.Source = new CroppedBitmap(fullBitmap, new Int32Rect(0, 0, 100, 100));
dkozl
  • 32,814
  • 8
  • 87
  • 89
0

To build a little on dkozl's answer above,

I found the DPI of the original image to crop and the DPI of the display were creating an "offset" of where I was intending to crop the original image.

The "offset" can be corrected by matching the image Width and Height in pixels with the DPI of the screen, instead of letting the Image element to it's own automatic sizing. You do this by setting the Stretch to None, and the Width & Height of the image as below:

<Image
    x:Name="imageToCrop"
    Stretch="None"
    Width="{Binding Source.PixelWidth,RelativeSource={RelativeSource Self}}"
    Height="{Binding Source.PixelHeight,RelativeSource={RelativeSource Self}}"/>
Rafael Ventura
  • 284
  • 3
  • 13