Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】

Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】,第1张

概述Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】 在Silverlight中我们可以捕捉视频设备以制作视频会议系统,或者通过视频设备截图功能上传头像等功能。          下面我们通过一个简单的实例来访问视频设备,并且截取图像下载该截图文件至本地。          一、在Silverlight运行界面中我们检查系统默认摄像头和麦克风是

Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】

在Silverlight中我们可以捕捉视频设备以制作视频会议系统,或者通过视频设备截图功能上传头像等功能。

         下面我们通过一个简单的实例来访问视频设备,并且截取图像下载该截图文件至本地。

         一、在Silverlight运行界面中我们检查系统默认摄像头和麦克风是否可用如下图:

         二、我们看Xaml代码如下所示:

< GrID x:name = " LayoutRoot " Background = " White " >
< border borderBrush = " Silver " borderThickness = " 1 " Height = " 346 " HorizontalAlignment = " left "
margin
= " 21,19,0 " name = " border1 " VerticalAlignment = " top " WIDth = " 477 " >
< border.Background >
< VIDeoBrush x:name = " ShowVIDeo " ></ VIDeoBrush >
</ border.Background >
</ border >
< button Content = " 打开摄像头 " Height = " 32 " HorizontalAlignment = " left "
margin
= " 38,380,0 " name = " button1 " VerticalAlignment = " top "
WIDth
= " 95 " Click = " button1_Click " />
< button Content = " 关闭摄像头 " Height = " 32 " HorizontalAlignment = " left "
name
= " button2 " WIDth = " 85 " VerticalAlignment = " top "
margin
= " 268,0 " Click = " button2_Click " />
< button Content = " 截 图 " Height = " 32 " name = " button3 " margin = " 153,0 "
HorizontalAlignment
= " left " WIDth = " 91 " VerticalAlignment = " top "
Click
= " button3_Click " />
< StackPanel Height = " 346 " HorizontalAlignment = " left " margin = " 514,0 "
name
= " stackPanel1 " VerticalAlignment = " top " WIDth = " 460 " />
</ GrID >

        在这里我们建立一个border显示视频图像,然后加三个按钮分别控制摄像头的打开、关闭、截图。最后加一个StackPanel来显示截图的影像。

        三、下面请看CS代码如下所示,对于截图保存图片所用函数是在园子里的zhangxuguang2007兄弟那里找的。

public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
// 提供音频和视频的方法
CaptureSource vIDeo = new CaptureSource();

private voID button1_Click( object sender,RoutedEventArgs e)
{
// 获取计算机上的默认视频对象
VIDeoCaptureDevice camera = CaptureDeviceConfiguration.GetDefaultVIDeoCaptureDevice();
// 成功请求到计算机上的视频设备
if (CaptureDeviceConfiguration.RequestDeviceAccess())
{
// 设置视频设备为Camera
vIDeo.VIDeoCaptureDevice = camera;
// VIDeoBrush设置源为vIDeo
ShowVIDeo.SetSource(vIDeo);
ShowVIDeo.Stretch
= Stretch.Fill;
// 开始捕捉视频
vIDeo.Start();
}
}

private voID button3_Click( object sender,RoutedEventArgs e)
{
// 截图
WriteableBitmap wBitmap = new WriteableBitmap(border1, new Matrixtransform());
Image img
= new Image();
img.WIDth
= 450 ;
img.margin
= new Thickness( 2 );
img.source
= wBitmap;
// 保存图片
if (wBitmap != null )
{
SavefileDialog saveDlg
= new SavefileDialog();
saveDlg.Filter
= " JPEG files (*.jpeg)|*.jpeg " ;
saveDlg.DefaultExt
= " .jpeg " ;

if (( bool )saveDlg.ShowDialog())
{
using (Stream fs = saveDlg.Openfile())
{
Savetofile(wBitmap,fs);
MessageBox.Show(
" 图片保存成功 " );
}
}
}
this .stackPanel1.Children.Clear();
this .stackPanel1.Children.Add(img);
}
/// <summary>
/// 保存图片,
/// </summary>
/// <param name="bitmap"></param>
/// <param name="fs"></param>
private static voID Savetofile(WriteableBitmap bitmap,Stream fs)
{
int wIDth = bitmap.PixelWIDth;
int height = bitmap.PixelHeight;
int bands = 3 ;
byte [][,] raster = new byte [bands][,];

for ( int i = 0 ; i < bands; i ++ )
{
raster[i]
= new byte [wIDth,height];
}

for ( int row = 0 ; row < height; row ++ )
{
for ( int column = 0 ; column < wIDth; column ++ )
{
int pixel = bitmap.Pixels[wIDth * row + column];
raster[
0 ][column,row] = ( byte )(pixel >> 16 );
raster[
1 ][column,row] = ( byte )(pixel >> 8 );
raster[
2 ][column,row] = ( byte )pixel;
}

}

FluxJpeg.Core.colorModel model
= new FluxJpeg.Core.colorModel
{ colorspace
= FluxJpeg.Core.colorSpace.RGB };
FluxJpeg.Core.Image img
= new FluxJpeg.Core.Image(model,raster);


// Encode the Image as a JPEG
MemoryStream stream = new MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder
=
new FluxJpeg.Core.Encoder.JpegEncoder(img, 100 ,stream);
encoder.Encode();

// Back to the start
stream.Seek( 0 ,SeekOrigin.Begin);

// Get teh Bytes and write them to the stream
byte [] binaryData = new Byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0 ,( int )stream.Length);
fs.Write(binaryData,binaryData.Length);
}
private voID button2_Click( object sender,RoutedEventArgs e)
{
// 停止视频
vIDeo.Stop();
}
}

        四、下面我们看看实际的运行效果如何,以及保存下文档的图分别如下所示,如需源码请点击 SL4Video.zip 下载:

分类: Silverlight实用窍门序列 总结

以上是内存溢出为你收集整理的Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】全部内容,希望文章能够帮你解决Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地【附带实例源码】所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074233.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存