日期:2014-05-18  浏览次数:20794 次

(急切求助)C++版视频切割修改成C#版。。。
下面的代码用到跟海康采集卡相关的DLL(Version4.3),
想把C++程序修改成C#版,请大家帮忙看下,谢谢。。。

void CCutFile::OnView()
{
DWORD nType;
ZeroMemory(&m_strRealBegin,sizeof(FRAME_POS)); //?????
ZeroMemory(&m_strRealBegin,sizeof(FRAME_POS));

if(m_nType==0) //根据贞的起始和结束索引来切割
nType=BY_FRAMENUM;

else //根据开始和结束时间来切割
{
nType=BY_FRAMETIME;
m_nBegin*=1000;
m_nEnd*=1000;
}

//locate the I-Frame .
  //调用海康SDK中的方法
Hik_PlayM4_GetKeyFramePos(PORT,m_nBegin,nType,&m_strRealBegin); 

if(!Hik_PlayM4_GetNextKeyFramePos(PORT,m_nEnd,nType,&m_strRealEnd))
{
m_strRealEnd.nFilePos=SetFilePointer(m_hPlayFile,0,0,FILE_END);
m_strRealEnd.nFrameNum=m_nMaxFrameNum;
m_strRealEnd.nFrameTime=m_nMaxTime*1000;
}

DWORD nBegin=0,nEnd=0; //初始值为0 
if(nType==BY_FRAMENUM) //设置真的开始和结束索引
{
nBegin = m_strRealBegin.nFrameNum;
nEnd = m_strRealEnd.nFrameNum;
}
else if(nType==BY_FRAMETIME) //设置开始和结束时间
{
nBegin = m_strRealBegin.nFrameTime/1000;
nEnd = m_strRealEnd.nFrameTime/1000;
}

else
MessageBox("Type is unknown");

CString csReal;
csReal.Format("%d",nBegin);
GetDlgItem(IDC_REAL_BEGIN)->SetWindowText(csReal); //给文本框赋值

csReal.Format("%d",nEnd);
GetDlgItem(IDC_REAL_END)->SetWindowText(csReal);
}

void CCutFile::OnSave()
{
OnView(); //预览

int nNewFileLen=int(m_strRealEnd.nFilePos-m_strRealBegin.nFilePos);

if(nNewFileLen<=0)
{
MessageBox("Stop position less than start position");
return ;
}

//UpdateData(TRUE);
HANDLE hNewFile=CreateFile( //获取文件句柄
m_csSaveFileName,
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

if(m_hPlayFile==INVALID_HANDLE_VALUE) //判断文件是否正常打开
{
CString csErrorName;
csErrorName.Format("Create file:%s Faild",m_csSaveFileName);
MessageBox(csErrorName);
return;
}

//Copy file 拷贝文件
DWORD nSize=2048;//Per 2k data;
PBYTE pBuf=new BYTE [nSize];
DWORD nFileHeadLen= Hik_PlayM4_GetFileHeadLength(); //获取文件头长度
DWORD nRet;
DWORD nBlock=nNewFileLen/nSize;
DWORD nRemain=nNewFileLen%nSize;
DWORD nCount;

//file header 写文件头
SetFilePointer(m_hPlayFile,0,0,FILE_BEGIN); //根据句柄,移动文件指针

if(!ReadFile(m_hPlayFile,pBuf,nFileHeadLen,&nRet,NULL)) //读文件失败
{
MessageBox("Read file faild");
goto end;
}
else
{
if(!WriteFile(hNewFile,pBuf,nFileHeadLen,&nRet,NULL)) //写文件失败
{
MessageBox("Write file faild");
goto end;
}
}
   
SetFilePointer(m_hPlayFile,m_strRealBegin.nFilePos,0,FILE_BEGIN);

  //写文件内容
for(nCount=0;nCount<nBlock;nCount++)
{
if(!ReadFile(m_hPlayFile,pBuf,nSize,&nRet,NULL)) //读文件失败
{
MessageBox("Read file faild");
break;
}
if(!WriteFile(hNewFile,pBuf,nSize,&nRet,NULL)) //写文件失败
{
MessageBox("Write file faild");
break;
}
}

  //写文件尾部
if(!ReadFile(m_hPlayFile,pBuf,nRemain,&nRet,NULL)) //读文件失败
{
MessageBox("Read file faild");
}
else
{
if(!WriteFile(hNewFile,pBuf,nRemain,&nRet,NULL)) //写文件失败
MessageBox("Write file faild");
}
end:
delete []pBuf; //释放资源
CloseHandle(hNewFile); //释放句柄
}

------解决方案--------------------
参考