如何使用C#调用gdi.dll提供的API,给我一个例子
时间: 2023-03-04 14:01:36 浏览: 179
使用C语言有许多不同的方法,但其基本原理都是相同的:编写代码,编译代码,运行程序。编写 C 语言代码需要使用一种编辑器,如 Visual Studio Code 或 Sublime Text。编译 C 语言代码则需要使用一个编译器,如 GCC 或 Clang。最后,可以使用终端运行编译后的程序。
相关问题
帮我用C#写一个海康采集图像再传给VisionPro窗口的类
好的,以下是一个简单的C#类,用于从海康相机采集图像,并将图像传递给VisionPro窗口显示:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HCNetSDK;
using System.Drawing;
using System.Drawing.Imaging;
using VisionPro;
namespace HikvisionVisionPro
{
public class HikvisionCamera
{
private IntPtr m_lRealHandle = IntPtr.Zero;
private CHCNetSDK.NET_DVR_DEVICEINFO_V30 m_struDeviceInfo;
private CHCNetSDK.NET_DVR_PREVIEWINFO m_struPreviewInfo;
private uint m_iLastErr = 0;
private bool m_bInitSDK = false;
private bool m_bRecord = false;
private bool m_bSaveRealData = false;
private int m_iPlayHandle = -1;
private int m_iPort = -1;
private int m_iWidth = 0;
private int m_iHeight = 0;
private byte[] m_bySaveRealData = new byte[1024 * 1024 * 100];
private int m_iSaveRealDataSize = 0;
private VDisplay m_display;
public HikvisionCamera()
{
m_struDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
m_struPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
}
public bool InitSDK()
{
bool result = CHCNetSDK.NET_DVR_Init();
if (result == false)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("SDK初始化失败,错误码:" + m_iLastErr);
return false;
}
else
{
m_bInitSDK = true;
return true;
}
}
public bool Login(string strIP, ushort wPort, string strUsername, string strPassword)
{
CHCNetSDK.NET_DVR_DEVICEINFO_V30 struDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
int iUserID = CHCNetSDK.NET_DVR_Login_V30(strIP, wPort, strUsername, strPassword, ref struDeviceInfo);
if (iUserID < 0)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("登录失败,错误码:" + m_iLastErr);
return false;
}
else
{
m_struDeviceInfo = struDeviceInfo;
return true;
}
}
public bool StartPreview(IntPtr hWnd)
{
m_struPreviewInfo.hPlayWnd = hWnd;
m_struPreviewInfo.lChannel = 1;
m_struPreviewInfo.dwStreamType = 0; // 0-主码流,1-子码流
m_struPreviewInfo.dwLinkMode = 0; // 0-TCP模式,1-UDP模式,2-多播模式,3-RTP模式,4-RTP/RTSP,5-RSTP/HTTP
m_struPreviewInfo.bBlocked = true;
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(0, ref m_struPreviewInfo, null, IntPtr.Zero);
if (m_lRealHandle < 0)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("预览失败,错误码:" + m_iLastErr);
return false;
}
else
{
return true;
}
}
public bool StopPreview()
{
if (m_lRealHandle >= 0)
{
if (CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle))
{
m_lRealHandle = IntPtr.Zero;
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("停止预览失败,错误码:" + m_iLastErr);
return false;
}
}
else
{
return true;
}
}
public bool CaptureImage(string strSavePath)
{
if (m_lRealHandle < 0)
{
MessageBox.Show("请先启动预览!");
return false;
}
CHCNetSDK.NET_DVR_JPEGPARA strPicpara = new CHCNetSDK.NET_DVR_JPEGPARA();
strPicpara.wPicQuality = 0;
strPicpara.wPicSize = 0;
bool result = CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lRealHandle, 1, ref strPicpara, strSavePath);
if (result == false)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("抓图失败,错误码:" + m_iLastErr);
return false;
}
else
{
return true;
}
}
public bool StartRecord(string strSavePath)
{
if (m_lRealHandle < 0)
{
MessageBox.Show("请先启动预览!");
return false;
}
if (m_bRecord == true)
{
MessageBox.Show("正在录像,请先停止录像!");
return false;
}
if (m_bSaveRealData == true)
{
MessageBox.Show("正在保存实时数据,请先停止保存!");
return false;
}
m_bRecord = true;
if (CHCNetSDK.NET_DVR_SaveRealData(m_lRealHandle, strSavePath))
{
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("启动录像失败,错误码:" + m_iLastErr);
m_bRecord = false;
return false;
}
}
public bool StopRecord()
{
if (m_bRecord == false)
{
MessageBox.Show("没有在录像!");
return false;
}
m_bRecord = false;
if (CHCNetSDK.NET_DVR_StopSaveRealData(m_lRealHandle))
{
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("停止录像失败,错误码:" + m_iLastErr);
return false;
}
}
public void SetDisplay(VDisplay display)
{
m_display = display;
}
public void StartDisplay()
{
if (m_iPlayHandle > 0)
{
MessageBox.Show("正在播放,请先停止!");
return;
}
if (m_iPort < 0)
{
m_iPort = CHCNetSDK.NET_DVR_GetRealPlayerIndex();
}
if (m_display != null)
{
m_display.Start();
}
if (CHCNetSDK.NET_DVR_RigisterDrawFun(m_iPort, DrawFun, IntPtr.Zero))
{
if (CHCNetSDK.NET_DVR_PlayBackByPreview(m_lRealHandle, m_iPort))
{
m_iPlayHandle = m_iPort;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("启动播放失败,错误码:" + m_iLastErr);
}
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("注册回调函数失败,错误码:" + m_iLastErr);
}
}
public void StopDisplay()
{
if (m_iPlayHandle > 0)
{
CHCNetSDK.NET_DVR_StopPlayBack(m_iPlayHandle);
m_iPlayHandle = -1;
}
if (m_display != null)
{
m_display.Stop();
}
}
private void DrawFun(int lRealHandle, IntPtr hDc, uint dwUser)
{
if (m_display != null)
{
Bitmap bmp = GetBitmap(lRealHandle);
m_display.DrawBitmap(bmp);
}
}
private Bitmap GetBitmap(int lRealHandle)
{
IntPtr pBitmap = IntPtr.Zero;
CHCNetSDK.NET_DVR_GetBitmap(lRealHandle, ref pBitmap);
Bitmap bmp = Bitmap.FromHbitmap(pBitmap);
DeleteObject(pBitmap);
return bmp;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern bool DeleteObject(IntPtr hObject);
}
}
```
使用方法:
1. 在你的项目中添加VisionPro和海康相机SDK的引用,并将HCNetSDK.dll文件复制到你的项目目录下。
2. 创建一个HikvisionCamera对象,并调用InitSDK方法初始化SDK。
```csharp
HikvisionCamera camera = new HikvisionCamera();
camera.InitSDK();
```
3. 调用Login方法登录相机。
```csharp
string strIP = "192.168.1.64";
ushort wPort = 8000;
string strUsername = "admin";
string strPassword = "123456";
camera.Login(strIP, wPort, strUsername, strPassword);
```
4. 调用StartPreview方法启动预览。
```csharp
IntPtr hWnd = pictureBox1.Handle;
camera.StartPreview(hWnd);
```
5. 调用StartDisplay方法启动图像显示。
```csharp
VDisplay display = new VDisplay(pictureBox1.Handle);
camera.SetDisplay(display);
camera.StartDisplay();
```
6. 调用StopDisplay方法停止图
GdiPlus.dll 怎么取bitmap的句柄
GDI+ (Graphics Device Interface Plus) 是 Microsoft 提供的一组用于创建图形和图像处理的库,其核心组件之一就是 Gdiplus.dll。在 C# 或其他.NET语言中,你可以通过以下步骤获取 Bitmap 对象的句柄:
1. 引入所需命名空间:
```csharp
using System.Drawing;
using System.Runtime.InteropServices;
```
2. 加载 Gdiplus 库(如果尚未加载):
```csharp
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool GdiplusStartup(ref GdiplusStartupInput startupInput,
out GdiplusToken token, IntPtr logFont);
```
3. 初始化 Gdiplus:
```csharp
GdiplusStartupInput input = new GdiplusStartupInput();
GdiplusToken token;
if (!GdiplusStartup(out token, ref input, IntPtr.Zero))
{
// 初始化失败,添加错误处理
throw new Exception("Failed to initialize GDI+");
}
try
{
using (Bitmap bitmap = new Bitmap("path_to_your_bitmap.jpg")); // 替换为实际的位图路径
IntPtr handle = bitmap.GetHbitmap(); // 获取位图句柄
// 现在可以使用handle操作位图,例如绘制到其他控件或保存到文件
}
finally
{
GdiplusShutdown(token); // 使用完毕后记得释放资源
}
```
4. 注意,在使用完句柄后,一定要调用 `GdiplusShutdown` 函数关闭 GDI+。
阅读全文