silverlight 获取取Media图片

本文介绍了一个使用WPF实现的视频捕获及像素级图像处理的应用案例。通过两个按钮分别实现了从当前视频帧中捕获图像并缩放显示,以及对捕获的图像进行像素级修改的功能。

界面:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值