Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use CroppedBitmap class in WPF to show just a portion of an image.

Here's an example code:

<Image Width="100" Height="100">
    <Image.Source>
        <CroppedBitmap Source="image.jpg" SourceRect="10 10 50 50"/>
    </Image.Source>
</Image>

Here, the SourceRect property specifies the portion of the image to be shown. The first two values (10, 10) specify the top left corner of the rectangle and the next two values (50, 50) specify its width and height.

You can also create a CroppedBitmap instance in code-behind and set it as the image source:

CroppedBitmap cropped = new CroppedBitmap(
    new BitmapImage(new Uri("image.jpg", UriKind.Relative)), // image source
    new Int32Rect(10, 10, 50, 50)); // source rectangle

Image myImage = new Image();
myImage.Source = cropped;

Note that this creates a new instance of the image every time it is loaded, which can cause memory issues if done frequently.