* Links: .NET Desktop - framework\wpf * Apply suggestions from code review Co-authored-by: Andy De George <[email protected]>
2.4 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| How to: Crop an Image | 03/30/2017 |
|
|
c6bba109-c6e7-4cf8-bfe6-9cf8d01bb4fc |
How to: Crop an Image
This example shows how to crop an image using xref:System.Windows.Media.Imaging.CroppedBitmap.
xref:System.Windows.Media.Imaging.CroppedBitmap is primarily used when encoding a cropped version of an image to save out to a file. To crop an image for display purposes see the How to: Create a Clip Region topic.
Example
The following [!INCLUDETLA#tla_xaml] defines resources used within the samples below.
[!code-xamlimageelementexample#CroppedXAML1]
The following example creates an image using a xref:System.Windows.Media.Imaging.CroppedBitmap as its source.
[!code-xamlimageelementexample#CroppedXAML2]
[!code-csharpimageelementexample#CroppedCSharp1] [!code-vbimageelementexample#CroppedCSharp1]
The xref:System.Windows.Media.Imaging.CroppedBitmap can also be used as the source of another xref:System.Windows.Media.Imaging.CroppedBitmap, chaining the cropping. Note that the xref:System.Windows.Media.Imaging.CroppedBitmap.SourceRect%2A uses values that are relative to the source cropped bitmap and not the initial image.
[!code-xamlimageelementexample#CroppedXAML3]
[!code-csharpimageelementexample#CroppedCSharp2] [!code-vbimageelementexample#CroppedCSharp2]