记海康摄像头获取保存截图的一个方法(c#)
先获取海康摄像头接口信息
引入模块
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;
using System.Net;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using System.Xml;
方法编写
private string GetMethodImage(string pathFile, byte[] imgbit)
{
if (!Directory.Exists(pathFile))
{
Directory.CreateDirectory(pathFile);
}
pathFile += $@"{DateTime.Now.ToString("yyMMddHHmmss")}.jpg";
WriteBytesToFile(pathFile, imgbit);
return pathFile;
}
public static void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
static byte[] GetUrlToImage(string Url, string User, string password)
{
System.Net.WebRequest WebRequest = System.Net.WebRequest.Create(Url);
WebRequest.Method = "GET";
NetworkCredential d = new NetworkCredential(User,password);
WebRequest.Credentials = d;
Byte[] filebyteArray;
try
{
using (WebResponse webRes = WebRequest.GetResponse())
{
int length = (int)webRes.ContentLength;
HttpWebResponse response = webRes as HttpWebResponse;
Stream stream = response.GetResponseStream();
MemoryStream MemoryStream = new MemoryStream();
byte[] buffer = new byte[length];
int i;
while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
{
MemoryStream.Write(buffer, 0, i);
}
filebyteArray = MemoryStream.ToArray();
MemoryStream.Close();
return filebyteArray;
}
}
catch {
return null;
}
}
方法调用
string STCD=“90000000”;
string pathFile = “C:\Users\Desktop\testpic” + string.Format("\{0}\{1}\{2}\", DateTime.Now.ToString(“yyyy”), DateTime.Now.ToString(“MMdd”),STCD);
string picurl = “http://IP:port/ISAPI/Streaming/channels/101/picture”;
string User = “admin”;
string password = “password”;
byte[] ImgBit = null;
ImgBit = GetUrlToImage(picurl, User,password);
string savepath = GetMethodImage(pathFile, ImgBit);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)