界面:
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="White">
<StackPanel Orientation="Horizontal" Margin="0,0,-117,0">
<Border Width="320" Height="240"
Margin="5" BorderBrush="Black"
BorderThickness="1">
<MediaElement x:Name="media" Source="medias/may.wmv"/>
</Border>
<Border Width="160" Height="120"
Margin="5" BorderBrush="Black"
BorderThickness="1">
<Image x:Name="CaptureImage1" Stretch="None"/>
</Border>
</StackPanel>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>
后台:
public MainPage()
{
InitializeComponent();
button1.Click += new RoutedEventHandler(CaptureImage);
button2.Click += new RoutedEventHandler(ModifyPixels);
}
private void CaptureImage(object sender, RoutedEventArgs e)
{
//Transform to scale the image by 50%
ScaleTransform transform = new ScaleTransform();
transform.ScaleX = .5;
transform.ScaleY = .5;
//Get bitmap image from the current frame of video
WriteableBitmap bitmap = new WriteableBitmap(media, transform);
//Set the Image object,defined in XAML,to the new bitmap.
CaptureImage1.Source = bitmap;
}
private void ModifyPixels(object sender,RoutedEventArgs e)
{
//Get WriteableBitmap.
if (CaptureImage1.Source != null)
{
WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)CaptureImage1.Source);
//Iterate through each pixel.
for (int x = 0; x < bitmap.Pixels.Length; x++)
{
//Set every 4th pixel.
if (x % 4 == 0)
{
bitmap.Pixels[x] = 0;
}
}
//Set Image object,defined in XAML ,to the modified bitmap.
CaptureImage1.Source = bitmap;
}
}
