如果需要查看更多文章,請微信搜索公眾號 csharp編程大全,需要進C#交流群群請加微信z438679770,備注進群, 我邀請你進群! ! !


鏈接:https://zhidao.baidu.com/question/559571801.html
C#中的IntPtr型別稱為“平臺特定的整數型別”,它們用于本機資源,如視窗句柄,資源的大小取決于使用的硬體和作業系統,但其大小總是足以包含系統的指標(因此也可以包含資源的名稱),
所以,在您呼叫的API函式中一定有類似表單句柄這樣的引數,那么當您宣告這個函式時,您應該將它顯式地宣告為IntPtr型別,
例如,在一個C#程式中呼叫Win32API mciSendString函式控制光碟驅動器,這個函式的函式原型是:
MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
首先在C#中宣告這個函式:
[DllImport("winmm.dll")]
private static extern long mciSendString(string a,string b,uint c,IntPtr d);
然后用這樣的方法呼叫:
mciSendString("set cdaudio door open", null, 0, this.Handle);
//----------------------------------------------------------------------------
// Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
namespace CameraCapture
{
public partial class CameraCapture : Form
{
private VideoCapture _capture = null;
private bool _captureInProgress;
private Mat _frame;
private Mat _grayFrame;
private Mat _smallGrayFrame;
private Mat _smoothedGrayFrame;
private Mat _cannyFrame;
public CameraCapture()
{
InitializeComponent();
//使用顯卡處理影像資料效率會很多,如果你的設備支持,最好打開,使用CvInvoke.HaveOpenCLCompatibleGpuDevice能回傳是否支持.
// 配置CvInvoke.UseOpenCL能讓OpenCV 啟用或者停用 GPU運算
//CvInvoke.UseOpenCL = CvInvoke.HaveOpenCLCompatibleGpuDevice;
CvInvoke.UseOpenCL = false;
try
{
//構造一個攝像頭實體,如果呼叫本地攝像機則括號里面為空
_capture = new VideoCapture(@"C:\Users\Administrator\Desktop\video\vtest.avi");
_capture.ImageGrabbed += ProcessFrame;//影像捕捉事件
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
_frame = new Mat();
_grayFrame = new Mat();
_smallGrayFrame = new Mat();
_smoothedGrayFrame = new Mat();
_cannyFrame = new Mat();
}
private void ProcessFrame(object sender, EventArgs arg)
{
if (_capture != null && _capture.Ptr != IntPtr.Zero)
// if (_capture != null)
{
_capture.Retrieve(_frame, 0);
CvInvoke.CvtColor(_frame, _grayFrame, ColorConversion.Bgr2Gray);
CvInvoke.PyrDown(_grayFrame, _smallGrayFrame);
//進行高斯向下采樣,執行高斯金字塔分解步驟向下采樣,固定原影像,
//洗掉指定行和列(可以全為奇數行和列,或者偶數行和列...),從而減小影像的寬度和高度,
CvInvoke.PyrUp(_smallGrayFrame, _smoothedGrayFrame);
//執行高斯金字塔分解向上采樣,首先透過注入固定行和列0像素值,在通過插值演算法,對插入行列進行插值,這樣用于放大原影像的四倍,
//引數決議:IInputArraysrc:輸入影像,即原影像,IOutputArraydst:輸出影像,采樣后得到的影像,
CvInvoke.Canny(_smoothedGrayFrame, _cannyFrame, 100, 60);
//多級邊緣檢測演算法
captureImageBox.Image = _frame;
grayscaleImageBox.Image = _grayFrame;
smoothedGrayscaleImageBox.Image = _smoothedGrayFrame;
cannyImageBox.Image = _cannyFrame;
}
}
private void captureButtonClick(object sender, EventArgs e)
{
if (_capture != null)
{
if (_captureInProgress)
{ //stop the capture
captureButton.Text = "Start Capture";
_capture.Pause();
}
else
{
//start the capture
captureButton.Text = "Stop";
_capture.Start();
}
_captureInProgress = !_captureInProgress;
}
}
//Dispose意為釋放,釋放組件所占用的記憶體,
//C#特性,為提高運行效率,自動會釋放已使用過且不再需要使用的組件來減少程式的CPU使用率,
//默認會在程式運行一段時間后自動加載該Dispose方法,或者可以顯式的自行呼叫此方法,
private void ReleaseData()
{
if (_capture != null)
_capture.Dispose();
}
private void FlipHorizontalButtonClick(object sender, EventArgs e)
{
// 得到和設定Capture類是否進行水平翻轉,
if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
}
private void FlipVerticalButtonClick(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/227014.html
標籤:.NET技术
上一篇:C# 視頻播放
下一篇:Accord.NET入門
