大佬教程收集整理的这篇文章主要介绍了Silverlight 截取摄像头视频截图并通过Base64String上传到数据库的例子,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
用Silverlight截取摄像头视频截图并不是什么难事。本文的范例代码详细介绍了视频截取和两种保存截图的方式:
1.使用开源的FJCore.dll 把视频流转换成Jpeg 格式,保存到本地,代码如下:
public static void EncodeJpeg(WriteableBitmap bmp,Stream dstStream) { // Init buffer in FluxJpeg format int w = bmp.pixelWidth; int h = bmp.pixelHeight; int[] p = bmp.pixels; byte[][,] pixelsForJpeg = new byte[3][,]; // RGB colors pixelsForJpeg[0] = new byte[w,h]; pixelsForJpeg[1] = new byte[w,h]; pixelsForJpeg[2] = new byte[w,h]; // Copy WriteableBitmap data into buffer for FluxJpeg int i = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int color = p[i++]; pixelsForJpeg[0][x,y] = (bytE)(color >> 16); // R pixelsForJpeg[1][x,y] = (bytE)(color >> 8); // G pixelsForJpeg[2][x,y] = (bytE)(color); // B } } //Encode Image as JPEG var jpegImage = new FluxJpeg.Core.Image(new ColorModel { colorspace = ColorSpace.RGB },pixelsForJpeg); var encoder = new JpegEncoder(jpegImage,95,dstStream); encoder.Encode(); }
//使用SaveFileDialog保存到本地 <pre name="code" class="csharp"> private void btSave_Click(object sender,RoutedEventArgs E) { if (saveFileDlg.ShowDialog().value) { try { using (Stream dstStream = saveFileDlg.openFile()) { WriteableBitmap bmp = new WriteableBitmap(ViewportHost,null); EncodeJpeg(bmp,dstStream); } } catch (Exception eX) { messageBox.Show(ex.ToString()); } } }
但如何上传到服务器,并保存在数据库内,作者尝试了多种方案,由于Silverlight 在代码安全方面的诸多限制,最终选择使用把图片转换成Base64String,
使用Base64String的方法,在数据库表里建一个varchar的字段就可以保存图片数据了。
2. 通过把图片流转换成Base64String保存到服务器数据库:
实现方法:
public class Base64Convert { public static String Encode(Stream stream) { try { BinaryReader binary = new BinaryReader(stream); Byte[] imgB = binary.ReadBytes((int)stream.Length); return Convert.ToBase64String(imgB); } catch { return ""; } } public static BitmapImage Decode(String s) { byte[] imageData = Convert.FromBase64String(s); MemoryStream ms = new MemoryStream(imageData); BitmapImage tempImage = new BitmapImage(); tempImage.Setsource(ms); ms.Dispose(); return tempImage; } }
private void OKButton_Click(object sender,RoutedEventArgs E) { if (_captureImage != null)//如果已经有抓拍的图像,那么可以上传 { this.txtBlog.Text = Base64Convert.Encode(SaveSnapshotToServer()); } //这时候,this.txtBlog.Text 内的Base64String 就可以保存在数据库的varCHAR字段内了! //如果要从数据库取回图片数据,那么就调用 // Image.source = Base64Convert.Decode("从数据库取回的String"); }
大家从上面的截图就可以看出,所截取的视频图像,已经转换成String格式,并可以保存在文本框里了。
这时候,this.txtBlog.Text 内的Base64String 就可以保存在数据库的varCHAR字段内了。 如果要从数据库取回图片数据,并显示在一个Image控件内,那么调用以下的方法即可: Image.source = Base64Convert.Decode("从数据库取回的String");
以上是大佬教程为你收集整理的Silverlight 截取摄像头视频截图并通过Base64String上传到数据库的例子全部内容,希望文章能够帮你解决Silverlight 截取摄像头视频截图并通过Base64String上传到数据库的例子所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。