先打开directshow播放mpeg流。然后如下:
procedure TMovieXForm.GrabButtonClick(Sender: TObject);
var
MediaType: TAM_MEDIA_TYPE
MediaPosition: Int64;
bmpHandle: HBITMAP;
bmp: TBitmap;
pBmpInfo: pBitmapInfo;
pVideoHeader: ^TVIDEOINFOHEADER;
buf : pointer;
iName, tpFileName: string;
bmSize,bmWidth,bmHeight: LongInt;
begin
If not Assigned(dsMediaControl) then
Exit;
if plState=stPlaying then
PlayButtonClick(Sender);
MediaPosition:= GetCurrentPosition;
If Assigned(dsSampleGrabber) then
begin
dsSampleGrabber.SetBufferSamples(TRUE);// 设立样例缓冲区
dsSampleGrabber.SetOneShot(TRUE);// 设立单步模式。
end;
SetCurrentPosition(MediaPosition);
//
dsSampleGrabber.GetConnectedMediaType(MediaType);
// 获取一个指向视频头的指针。
pVideoHeader:= MediaType.pbFormat;
// 视频头包含位图信息。// 将它拷贝到BITMAPINFO结构。
GetMem(pBmpInfo,sizeof(TBitmapInfo));
ZeroMemory(pBmpInfo,sizeof(TBitmapInfo));
CopyMemory(@(pBmpInfo^.bmiHeader),@(pVideoHeader^.bmiHeader),sizeOf(BITMAPINFOHEADER));
// 从位图头创建一个DIB并取得指向缓冲区的指针。
bmpHandle:= CreateDIBSection(0, pBmpInfo^, DIB_RGB_COLORS, buf, 0, 0);
// 将图象拷贝到缓冲区。
bmSize:= pVideoHeader^.bmiHeader.biSize;
bmWidth:= pVideoHeader^.bmiHeader.biWidth;
bmHeight:= pVideoHeader^.bmiHeader.biHeight;
showMessage(intToStr(bmSize)+'/'+intToStr(bmWidth)+'/'+intToStr(bmHeight));
dsSampleGrabber.GetCurrentBuffer(bmSize,buf);
// 将图象缓冲区存为BMP文件。//Error= TBitmap(bmpHandle).SaveToFile('c:/xj2000.bmp');
bmp:= TBitmap.Create;
bmp.Handle:= bmpHandle;
tpFileName:= Config.TmpPath+'/'+'grab.bmp';
bmp.SaveToFile(tpFileName);
bmp.Free;
deleteObject(bmpHandle);
FreeMem(pBmpInfo);
If Assigned(dsSampleGrabber) then
begin
dsSampleGrabber.SetBufferSamples(FALSE);// 设立样例缓冲区
dsSampleGrabber.SetOneShot(FALSE);// 设立单步模式。
end;
////////////////////////////////////////////////////////////////////////////////
iName:= _GetNewImageName;
EndoData.Query2.Append;
//EndoData.Query2.FieldByName('I_ID').AsInteger:= 0;
//Remarked by ADO
EndoData.Query2.FieldByName('M_ID').AsInteger:= EndoData.Query1.FieldByName('P_ID').AsInteger;
EndoData.Query2.FieldByName('I_IMAGE').AsString:= iName;
EndoData.Query2.FieldByName('I_X').AsInteger:= 0;
EndoData.Query2.FieldByName('I_Y').AsInteger:= 0;
EndoData.Query2.FieldByName('I_EXT').AsInteger:= 0;
//EndoData.Query2.FieldByName('I_PART').AsString:= '';//MyEdit21.Text;
//EndoData.Query2.FieldByName('I_REMARK').AsString:= '';//MyEdit22.Text;
EndoData.Query2.Post;
EndoData.Query2.Refresh;
_apiCopyFile(tpFileName,Config.TmpPath+'/'+iName+'.BMP');
_PackImageTo(iName,Config.TmpPath+'/',_GetNetImagePath+'/');
////////////////////////////////////////////////////////////////////////////////
MainForm.DBEdit1Change(Sender);//Refresh the Imagebar.
end;