Share via


방법: 이미지 잘라내기

업데이트: 2007년 11월

이 예제에서는 CroppedBitmap을 사용하여 이미지를 잘라내는 방법을 보여 줍니다.

CroppedBitmap은 이미지의 잘려진 버전을 인코딩하여 파일로 저장할 때 주로 사용됩니다. 표시를 위해 이미지를 잘라내는 방법에 대한 자세한 내용은 방법: 클립 영역 만들기 항목을 참조하십시오.

예제

다음 XAML(Extensible Application Markup Language)에서는 아래의 샘플에 사용된 리소스를 정의합니다.

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

다음 예제에서는 CroppedBitmap을 소스로 사용하여 이미지를 만듭니다.

<!-- 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
// 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

CroppedBitmap을 다른 CroppedBitmap의 소스로 사용하여 잘라내기 작업을 연속적으로 수행할 수 있습니다. SourceRect는 원래 이미지가 아니라 잘려진 소스 비트맵에 대해 상대적인 값을 사용합니다.

<!-- 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
// 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;

전체 샘플을 보려면 Image 요소 샘플을 참조하십시오.

참고 항목

작업

방법: 클립 영역 만들기

Image 요소 샘플