我正在使用Visual Studio 2013创建Windows应用程序表单以从我的IP摄像机获取图像,但是不幸的是,一旦单击该按钮,该图像就不会出现在图片框上。我该怎么做才能解决这个问题?
我尝试了各种参考,包括Emgu.Cv,AForge.net等,但这些参考都没有。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using Emgu.CV;
namespace WindowsFormsApp3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
getFrame();
}
private void getFrame()
{
string sourceURL = "http://admin:testground@192.xxx.x.x/video/mjpg.cgi";
byte[] buffer = new byte[1280 * 800];
int read, total = 0;
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(sourceURL);
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
while((read = stream.Read(buffer, total, 1000)) != 0)
{
total += read;
}
Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total));
pictureBox1.Image = bmp;
}
}
}
预期结果应该显示我的IP摄像机的视频,但实际输出是
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (401) Unauthorized.