1、Dephi实现屏幕录像Dephi实现屏幕录像delphi利用VFW函数实现录制屏幕的功能:1.通过Timer实现抓屏,将屏幕抓图存为BMP格式。2.调用AVIFileInit初始化3.调用AVIFileOpen创建视频文件4.调用AVIFileCreateStream创建视频流5.调用AVIMakeCompressedStream压缩视频流6.调用AVIStreamSetFormat设置视频流格式7.调用AVIStreamWrite写入视频8.调用AVIStreamClose关闭视频流指针,AVIFileRelease释放视频文件,AVIFileExit关闭视频文件。源码:1.抓屏存为BMP/
2、抓屏幕, Flag = SRCCOPY,不抓透明窗口,Flag=SRCCOPY or CAPTUREBLT抓透明窗口/CAPTUREBLT要自己定义/const/$ifndef CAPTUREBLT/ CAPTUREBLT = $40000;/$endifvarScreenBmp: TBitmap;PicIndex: Integer; /图片序号RecTransFlag: Cardinal; /录制屏幕标志,是否录制透明窗体AppPath: string; /应用程序路径TempPath: string; /临时文件夹路径BmpFileName: string; /BMP文件存储路径proce
3、dure TForm1.FormCreate(Sender: TObject);beginPicIndex := 0;BmpFileName := ;ScreenBmp := TBitmap.Create;ScreenBmp.PixelFormat := pf24bit;ScreenBmp.Width := Screen.Width;ScreenBmp.Height := Screen.Height;ScreenBmp.Canvas.Brush.Style := bsClear;RecPause := False;/不录制透明窗体RecTransFlag := SRCCOPY;/录制透明窗体/
4、RecTransFlag := SRCCOPY or CAPTUREBLT;AppPath := ExtractFilePath(Application.ExeName);TempPath := AppPath + Temp;if not DirectoryExists(TempPath) thenCreateDirectory(PAnsiChar(TempPath), nil);end;procedure TForm1.DrawCursorToBmp(var ScrBmp: TBitmap);varhCur : HICON;MousePt: TPoint;beginif not Assign
5、ed(ScrBmp) thenExit;hCur := GetCursor();GetCursorPos(MousePt);/画光标DrawIcon(ScrBmp.canvas.handle, MousePt.x, MousePt.y, hCur);end;procedure TForm1.Timer1Timer(Sender: TObject);vardc: HDC;beginDC := GetDC(0);tryBitBlt(ScreenBmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,dc, 0, 0, Flag);finallyRe
6、leaseDC(0, dc);end;/根据标记判断是否画上光标DrawCursorToBmp(ScreenBmp);BmpFileName := TempPath + IntToStr(PicIndex) + .bmp;ScreenBmp.SaveToFile(BmpFileName );end;tryInc(PicIndex);excepton EIntOverflow doend;procedure TForm1.Button1Click(Sender: TObject);begintimer1.Enabled := True;end;2.取保存的bmp图片生成avi并添加声音.uses
7、 VFW;varAviFileName: string; /AVI文件存储路径Tempbmp: TBitmap; /生成AVI前读出的BMPfunction GetBitmapData(bmp: TBitmap): TImageData;beginbmp.PixelFormat := pf24bit;Result.Width := bmp.Width;Result.Height := bmp.Height;Result.Stride := Result.Width shl 2;Result.Scan0 := bmp.ScanLineResult.Height - 1;Result.PixelF
8、ormat := -1; / Windows bitmap formatflagResult.Reserved := 0;end;procedure CreateAVI;varhr: Integer;Strhdr: TAVIStreamInfo; /AVI流信息Wavehdr: TWaveFormatEx;Opts: TAVICOMPRESSOPTIONS; /压缩选项POpts: PAVICOMPRESSOPTIONS; /压缩选项指针Bi: BITMAPINFOHEADER; /BMP头信息B: Boolean;P: Pointer;i: Integer;bmpData: TImageDa
9、ta;nRet : Integer;beginif FileExists(AviFileName) thenDeleteFile(AviFileName);AVIFileInit;hr:=AVIFileOpen(PFile,PChar(AviFileName),OF_CREATEorOF_WRITE,nil);if (hr 0) thenExit;/设置AVI流信息ZeroMemory(Strhdr, SizeOf(TAVIStreamInfo);strhdr.fccType := streamTypeVIDEO;strhdr.fccHandler := mmioFOURCC(D, I, V,
10、 X);strhdr.dwFlags := 0;strhdr.dwCaps := 0;strhdr.wPriority := 0;strhdr.wLanguage := 0;strhdr.dwScale := 1;strhdr.dwRate := 5; /pfsstrhdr.dwStart := 0;strhdr.dwLength := 0;strhdr.dwInitialFrames := 0;strhdr.dwSuggestedBufferSize := (Screen.Width * 3 + 3) and $FFFC)* screen.Height; /Screen.Height * s
11、creen.Width * 3;strhdr.dwQuality := 0;strhdr.dwSampleSize := 1;strhdr.rcFrame := Rect(0, 0, Screen.Width, Screen.Height);strhdr.dwEditCount := 0;strhdr.dwFormatChangeCount := 0;strhdr.szName0 := #0;strhdr.szName1 := #0;/设置WAV流信息ZeroMemory(Wavehdr, SizeOf(TWaveFormatEx);Wavehdr.cbSize := 0;Wavehdr.nC
12、hannels := 1;Wavehdr.wFormatTag:= 1;Wavehdr.wBitsPerSample := 16;Wavehdr.nSamplesPerSec := 8000;Wavehdr.nBlockAlign := Wavehdr.wBitsPerSample * Wavehdr.nChannelsdiv 8;Wavehdr.nAvgBytesPerSec := Wavehdr.nBlockAlign *Wavehdr.nSamplesPerSec;ZeroMemory(opts, SizeOf(TAVICOMPRESSOPTIONS);/设置压缩信息/Indeo? Vi
13、deo 5.10opts.fccType := streamTypeVIDEO; / vidsopts.fccHandler := 9;opts.dwKeyFrameEvery := 0;opts.dwQuality := 8500;opts.dwBytesPerSecond := 0;opts.dwFlags := 8;opts.lpFormat := nil;opts.cbFormat := 0;opts.cbParms := 48;opts.dwInterleaveEvery := 0;/设置BMP头信息ZeroMemory(bi, SizeOf(BITMAPINFOHEADER);bi
14、.biSize := SizeOf(bi);bi.biWidth := Screen.Width;bi.biHeight := Screen.Height;bi.biPlanes := 1;bi.biBitCount := 24;bi.biCompression := 0;bi.biSizeImage:=(Screen.Width*3+3)and$FFFC)*screen.Height;/(bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight /Screen.Height *screen.Width * 3; /_stride * _height;bi.
15、biXPelsPerMeter := 0;bi.biYPelsPerMeter := 0;bi.biClrUsed := 0;bi.biClrImportant := 0;hr := AVIFileCreateStream(PFile, PSrcStream, strhdr);if (hr 0) thenExit;hr:=AVIMakeCompressedStream(PCompStream,PSrcStream,opts,nil);if (hr 0) thenAbort;hr := AVIStreamSetFormat(PCompStream, 20, bi, 40);if (hr 0) t
16、henAbort;Tempbmp := TBitmap.Create;Tempbmp.LoadFromFile(bmpFile);bmpData := GetBitmapData(Tempbmp);hr := AVIStreamWrite(PCompStream,FrameCount, /存在第几帧1,bmpData.Scan0,(Screen.Width * 3 + 3) and $FFFC) *screen.Height,0,nil,nil);if (hr 0) thenAbort;/添加声音AddAVISound;end;end;procedure AddAVISound;varwfx:
17、 TWAVEFORMATEX;buf: array of char;wav: PWavChunk;hf: HWND;size: DWORD;Strhdr: TAVIStreamInfo;hr: Cardinal;numbytes, numsamps: DWORD;beginZeroMemory(wfx, sizeof(TWAVEFORMATEX);hf := CreateFile(PChar(WavFileName), GENERIC_READ, FILE_SHARE_READ,nil, OPEN_EXISTING, 0, 0);if (hf = 0) thenExit;trysize :=
18、GetFileSize(hf, nil);SetLength(buf, size);ReadFile(hf, buf0, size, size, nil);finallyCloseHandle(hf);end;wav := PWavChunk(buf);wfx.wFormatTag := wav.fmt.wFormatTag;wfx.cbSize := 0;wfx.nAvgBytesPerSec := wav.fmt.dwAvgBytesPerSec;wfx.nBlockAlign := wav.fmt.wBlockAlign;wfx.nChannels := wav.fmt.wChannel
19、s;wfx.nSamplesPerSec := wav.fmt.dwSamplesPerSec;wfx.wBitsPerSample := wav.fmt.wBitsPerSample;ZeroMemory(Strhdr, SizeOf(TAVIStreamInfo);Strhdr.fccType := streamtypeAUDIO;Strhdr.dwScale := wfx.nBlockAlign;Strhdr.dwRate := wfx.nSamplesPerSec * wfx.nBlockAlign;Strhdr.dwSampleSize := wfx.nBlockAlign;Strh
20、dr.dwQuality := DWORD(-1);hr := AVIFileCreateStream(PFile, PWavStream, Strhdr);if (hr 0) thenbeginif (buf nil) thenbuf := nil;Exit;end;hr:=AVIStreamSetFormat(PWavStream,0,wfx,SizeOf(TWAVEFORMATEX);if (hr 0) thenbeginif (buf nil) thenbuf := nil;Exit;end;/ now we can write the datanumbytes := wav.dat.size;if wfx.wBitsPerSample 0 thennumsamps := numbytes * 8 div wfx.wBitsPerSampleelsenumsamps := 0;hr := AVIStreamWrite(PWavStream, 0, numsamps, wav, numbytes, 0, nil,nil);if (buf nil) thenbuf := nil;if (hr 0) thenExit;WavAddCompleted := True;end;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1