Kinect初探之二
今天研究了一下KinectColorImage,SDK里给的demo是WPF的,感觉通用性太低,于是用C# Forms实现了一下。弄了半天,确实能获取到colorImage,并用Marshal.Copy的方法把bytes变成bitmap,但是麻烦的是没办法用form_paint或者picturebox.Image显示出来,回头再研究一下吧。
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Microsoft.Kinect;
10 using System.Drawing.Imaging;
11
12 namespace test1
13 {
14 public partial class Form1 : Form
15 {
16 static KinectSensor kinectSensor;// = new KinectSensor();
17 static Bitmap bitmap;
18 static int count = 0;
19 public Form1()
20 {
21 InitializeComponent();
22
23 }
24
25 private void Form1_Load(object sender, EventArgs e)
26 {
27 kinectSensor = (from item in KinectSensor.KinectSensors where item.Status == KinectStatus.Connected select item).FirstOrDefault();
28 kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
29 try
30 {
31 kinectSensor.Start();
32 }
33 catch (System.Exception ex)
34 {
35
36 }
37 kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
38 kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
39
40 }
41 private void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
42 {
43 using (ColorImageFrame cif = e.OpenColorImageFrame())
44 {
45 if (cif!=null)
46 {
47 byte[] data = new byte[cif.PixelDataLength];
48 cif.CopyPixelDataTo(data);
49 Rectangle rect = new Rectangle(0, 0, cif.Width, cif.Height);
50 bitmap = new Bitmap(cif.Width, cif.Height);
51 BitmapData bd = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
52 IntPtr ptr = bd.Scan0;
53 System.Runtime.InteropServices.Marshal.Copy(data,0,ptr,data.Length);
54 bitmap.UnlockBits(bd);
55 //this.pictureBox1.Image = bitmap;
56 //this.pictureBox1.Refresh();
57 //this.Form1_Paint(sender, e);
58 //this.BackgroundImage = bitmap;
59 Invalidate();
60 //Application.DoEvents();
61 if (count < 20)
62 {
63 bitmap.Save(/*DateTime.Now.Millisecond.ToString()*/count.ToString() + ".bmp", ImageFormat.Bmp);
64 count++;
65 }
66
67 }
68 }
69 }
70
71 private void Form1_Paint(object sender, PaintEventArgs e)
72 {
73 Graphics g = e.Graphics;
74 if (bitmap != null)
75 {
76 //bitmap.Save(DateTime.Now.Millisecond.ToString() + ".bmp", ImageFormat.Bmp);
77 //g.DrawImage(bitmap, ClientRectangle);
78 //g.DrawImage(DateTime.Now.Millisecond.ToString() + ".bmp",ClientRectangle);
79 }
80 }
81
补充:综合编程 , 其他综合 ,