我正在尝试在XNA中使用网络摄像头。我已成功在Windows窗体上创建了网络摄像头。现在我想在XNA中使用相同的代码。在Windows窗体中我使用pictureBox来显示网络摄像头视频。我想在XNA中使用pictureBox。
我已导入System.Drawing
库并将其用作using Bitmap = System.Drawing.Bitmap;
我正在使用AForge Framework。以下是我用Windows Form编写的代码。
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
string DeviceName;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
DeviceName= VideoCaptureDevice.Name;
}
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
// MessageBox.Show(DeviceName);
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// throw new NotImplementedException();
Bitmap image = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = image;
}
-
我在XNA中编写了几乎相同的代码,并且它成功地向我显示了CAMERA名称。请告诉我如何使用图片框或任何其他方式显示网络摄像头视频
XNA-CODE
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//WebCamVideo - Start
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
DeviceName = VideoCaptureDevice.Name;
}
myfont = Content.Load<SpriteFont>("myfont");
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
//WebCamVideo- End
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
//In Forms I used Picuture Box here to Display the Webcam video
}
答案 0 :(得分:1)
这是一个将位图转换为Texture2D的函数。
Texture2D BitmapToTexture(Bitmap bmap)
{
Mircosoft.Xna.Framework.Color[] colors = new Color[bmap.Width * bmap.Height];
for (int x = 0; x < bmap.Width; x++)
{
for (int y = 0; y < bmap.Height; y++)
{
int index = x + y * bmap.Width;
System.Color color = bmap.GetPixel(x, y);
Vector4 colorVector =
new Vector4((float)color.R / 255f,
(float)color.G / 255f,
(float)color.B / 255f, 1);
colors[index] = Color.FromNonPremultiplied(colorVector);
}
}
Texture2D texture = new Texture2D(GraphicsDevice, bmap.Width, bmap.Height);
texture.SetData<Color>(colors);
return texture;
}