Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
CroppedBitmap Class
 Source Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
CroppedBitmap..::.Source Property

Gets or sets the source for the bitmap. This is a dependency property.

Namespace:  System.Windows.Media.Imaging
Assembly:  PresentationCore (in PresentationCore.dll)
Visual Basic (Declaration)
Public Property Source As BitmapSource
Visual Basic (Usage)
Dim instance As CroppedBitmap
Dim value As BitmapSource

value = instance.Source

instance.Source = value
C#
public BitmapSource Source { get; set; }
Visual C++
public:
property BitmapSource^ Source {
    BitmapSource^ get ();
    void set (BitmapSource^ value);
}
JScript
public function get Source () : BitmapSource
public function set Source (value : BitmapSource)
XAML
For XAML information, see the BitmapSource type.

Property Value

Type: System.Windows.Media.Imaging..::.BitmapSource
The source for the bitmap. The default value is nullNothingnullptra null reference (Nothing in Visual Basic).

Identifier field

SourceProperty

Metadata properties set to true

None

You can chain a CroppedBitmap to another BitmapSource by setting the Source property of the CroppedBitmap to the appropriate BitmapSource. For more information about chaining, see How to: Chain BitmapSource Objects Together.

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 How to: Create a Clip Region topic.

The following Extensible Application Markup Language (XAML) defines resources used within the samples below.

XAML
<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.

XAML
<!-- Use the cropped image resource as the images source -->
<Image Width="200" Source="{StaticResource croppedImage}" 
   Margin="5" Grid.Column="0" Grid.Row="1" />
Visual Basic
' 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
C#
// Create an Image element.
Image croppedImage = new Image();
croppedImage.Width = 200;
croppedImage.Margin = new Thickness(5);

// Create a CroppedBitmap based off of a xaml defined resource.
CroppedBitmap cb = new CroppedBitmap(     
   (BitmapSource)this.Resources["masterImage"],
   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.

XAML
<!-- 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>
Visual Basic
' 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
C#
// Create an Image element.
Image chainImage = new Image();
chainImage.Width = 200;
chainImage.Margin = new Thickness(5);

// Create the cropped image based on previous CroppedBitmap.
CroppedBitmap chained = new CroppedBitmap(cb,
   new Int32Rect(30, 0, (int)cb.Width-30, (int)cb.Height)); 
// 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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker