CroppedBitmap Class
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
'Declaration Public NotInheritable Class CroppedBitmap Inherits BitmapSource Implements ISupportInitialize 'Usage Dim instance As CroppedBitmap
public final class CroppedBitmap extends BitmapSource implements ISupportInitialize
public final class CroppedBitmap extends BitmapSource implements ISupportInitialize
<CroppedBitmap .../>
CroppedBitmap implements the ISupportInitialize interface to optimize initialization on multiple properties. Property changes can only occur during object initialization. Call BeginInit to signal that initialization has begun and EndInit to signal that initialization has completed. Once initialized, property changes are ignored.
CroppedBitmap objects created using the CroppedBitmap(BitmapSource,Int32Rect) constructor are automatically initialized and properties cannot be changed.
Metadata tags related to image data must be updated if an image is saved to a file after a transform is applied.
Use the SourceRect property of this class to define the area of the bitmap that is to be cropped.
This example shows how to crop an image using CroppedBitmap.
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 Create a Clip Region topic.
The following Extensible Application Markup Language (XAML) defines resources used within the samples below.
<Page.Resources> <!-- Define some image resources, for use as the image element source. --> <BitmapImage x:Key="masterImage" UriSource="/sampleImages/gecko.jpg" /> <CroppedBitmap x:Key="croppedImage" Source="{StaticResource masterImage}" SourceRect="30 20 105 50"/> </Page.Resources>
<Page.Resources> <!-- Define some image resources, for use as the image element source. --> <BitmapImage x:Key="masterImage" UriSource="/sampleImages/gecko.jpg" /> <CroppedBitmap x:Key="croppedImage" Source="{StaticResource masterImage}" SourceRect="30 20 105 50"/> </Page.Resources>
The following example creates an image using a CroppedBitmap as its source.
<!-- Use the cropped image resource as the images source --> <Image Width="200" Source="{StaticResource croppedImage}" Margin="5" Grid.Column="0" Grid.Row="1" />
<!-- Use the cropped image resource as the images source --> <Image Width="200" Source="{StaticResource croppedImage}" Margin="5" Grid.Column="0" Grid.Row="1" />
' Create an Image element. Dim croppedImage As New Image() croppedImage.Width = 200 croppedImage.Margin = New Thickness(5) ' Create a CroppedBitmap based off of a xaml defined resource. Dim cb As New CroppedBitmap(CType(Me.Resources("masterImage"), BitmapSource), New Int32Rect(30, 20, 105, 50)) 'select region rect croppedImage.Source = cb 'set image source to cropped
The CroppedBitmap can also be used as the source of another CroppedBitmap, chaining the cropping. Note that the SourceRect uses values that are relative to the source cropped bitmap and not the initial image.
<!-- Chain a cropped bitmap off a previosly defined cropped image --> <Image Width="200" Grid.Column="0" Grid.Row="3" Margin="5"> <Image.Source> <CroppedBitmap Source="{StaticResource croppedImage}" SourceRect="30 0 75 50"/> </Image.Source> </Image>
<!-- Chain a cropped bitmap off a previosly defined cropped image --> <Image Width="200" Grid.Column="0" Grid.Row="3" Margin="5"> <Image.Source> <CroppedBitmap Source="{StaticResource croppedImage}" SourceRect="30 0 75 50"/> </Image.Source> </Image>
' Create an Image element. Dim chainImage As New Image() chainImage.Width = 200 chainImage.Margin = New Thickness(5) ' Create the cropped image based on previous CroppedBitmap. Dim chained As New CroppedBitmap(cb, New Int32Rect(30, 0, CType(cb.Width, Integer) - 30, CType(cb.Height, Integer))) ' Set the image's source. chainImage.Source = chained
For the complete sample, see Image Element Sample.
More Code
| How to: Chain BitmapSource Objects Together | This example shows how you can apply a variety of effects to an image source by chaining multiple BitmapSource derived objects together. |
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.