/// <summary> /// 创建流文件 /// </summary> private void CreateStream() { AVISTREAMINFOW strhdr = new AVISTREAMINFOW(); strhdr.fccType = _fccType; strhdr.fccHandler = _fccHandler; strhdr.dwFlags = 0; strhdr.dwCaps = 0; strhdr.wPriority = 0; strhdr.wLanguage = 0; strhdr.dwScale = 1; strhdr.dwRate = _frameRate; strhdr.dwStart = 0; strhdr.dwLength = 0; strhdr.dwInitialFrames = 0; strhdr.dwSuggestedBufferSize = _height * _stride; strhdr.dwQuality = 0xffffffff; strhdr.dwSampleSize = 0; strhdr.rect_top = 0; strhdr.rect_left = 0; strhdr.rect_bottom = _height; strhdr.rect_right = _width; strhdr.dwEditCount = 0; strhdr.dwFormatChangeCount = 0; strhdr.szName0 = 0; strhdr.szName1 = 0; int hr = AVIFileCreateStream(_pfile, out _ps, ref strhdr); if (hr != 0) { throw new AviException("AVIFileCreateStream"); } }
/** * Creates an AVI stream * @return void * @private */ private void CreateStream() { AVISTREAMINFOW strhdr = new AVISTREAMINFOW(); strhdr.fccType = fccType_; strhdr.fccHandler = fccHandler_; strhdr.dwFlags = 0; strhdr.dwCaps = 0; strhdr.wPriority = 0; strhdr.wLanguage = 0; strhdr.dwScale = 1; strhdr.dwRate = frameRate_; // Frames per Second strhdr.dwStart = 0; strhdr.dwLength = 0; strhdr.dwInitialFrames = 0; strhdr.dwSuggestedBufferSize = height_ * stride_; strhdr.dwQuality = 0xffffffff; //-1; // Use default strhdr.dwSampleSize = 0; strhdr.rect_top = 0; strhdr.rect_left = 0; strhdr.rect_bottom = height_; strhdr.rect_right = width_; strhdr.dwEditCount = 0; strhdr.dwFormatChangeCount = 0; strhdr.szName0 = 0; strhdr.szName1 = 0; int hr = AVIFileCreateStream(pfile_, out ps_, ref strhdr); if (hr != 0) { throw new AviException("AVIFileCreateStream"); } }
private static extern int AVIFileCreateStream(int ptr_pfile, out IntPtr ptr_ptr_avi, ref AVISTREAMINFOW ptr_streaminfo);
private void CreateStream() { AVISTREAMINFOW strhdr = new AVISTREAMINFOW(); strhdr.fccType = fccType_; strhdr.fccHandler = fccHandler_; strhdr.dwFlags = 0; strhdr.dwCaps = 0; strhdr.wPriority = 0; strhdr.wLanguage = 0; strhdr.dwScale = 1; strhdr.dwRate = frameRate_; // Frames per Second strhdr.dwStart = 0; strhdr.dwLength = 0; strhdr.dwInitialFrames = 0; strhdr.dwSuggestedBufferSize = height_ * stride_; strhdr.dwQuality = 0xffffffff; //-1; // Use default strhdr.dwSampleSize = 0; strhdr.rect_top = 0; strhdr.rect_left = 0; strhdr.rect_bottom = height_; strhdr.rect_right = width_; strhdr.dwEditCount = 0; strhdr.dwFormatChangeCount = 0; strhdr.szName0 = 0; strhdr.szName1 = 0; int hr = AVIFileCreateStream(pfile_, out ps_, ref strhdr); if (hr != 0) { throw new AviException("AVIFileCreateStream"); } }
private static extern int AVIFileCreateStream( int ptr_pfile, out IntPtr ptr_ptr_avi, ref AVISTREAMINFOW ptr_streaminfo);
private static extern int AVIFileCreateStream(IntPtr pfile, out IntPtr ppavi, ref AVISTREAMINFOW psi);
/// <summary>Initialize the AviFile.</summary> /// <param name="path">The path to the output file.</param> /// <param name="frameRate">The frame rate for the video.</param> /// <param name="width">The width of the video.</param> /// <param name="height">The height of the video.</param> /// <param name="quality">Video quality 0 to 10000.</param> public AviWriter(string path, int frameRate, int width, int height, uint quality = 10000) { #region Validation if (path == null) { throw new ArgumentNullException(nameof(path)); } if (frameRate <= 0) { throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "The frame rate must be at least 1 frame per second."); } if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), width, "The width must be at least 1."); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), height, "The height must be at least 1."); } #endregion //Store parameters var fccType = GetFourCc("vids"); _width = width; _height = height; _disposed = false; //Get the stride information by creating a new bitmap and querying it using (var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); _stride = (uint)bmpData.Stride; bmp.UnlockBits(bmpData); } try { //Initialize the AVI library. AVIFileInit(); //Open the output AVI file. var rv = AVIFileOpenW(ref _aviFile, path, AVI_OPEN_MODE_CREATEWRITE, 0); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } //Create a new stream in the avi file. var aviStreamInfo = new AVISTREAMINFOW { fccType = fccType, fccHandler = 0, dwScale = 1, dwRate = (uint)frameRate, dwSuggestedBufferSize = (uint)(_height * _stride), dwQuality = quality, //-1 default 0xffffffff, 0 to 10.000 rcFrame = new Native.Rect { Bottom = _height, Right = _width } }; rv = AVIFileCreateStream(_aviFile, out _aviStream, ref aviStreamInfo); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } //Configure the compressed stream. var streamFormat = new BITMAPINFOHEADER { biSize = 40, biWidth = _width, biHeight = _height, biPlanes = 1, biBitCount = 24, biSizeImage = (uint)(_stride * _height) }; rv = AVIStreamSetFormat(_aviStream, 0, ref streamFormat, 40); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); //, "Unable to set the AVI stream format."); } } catch { // Clean up Dispose(false); try { if (File.Exists(path)) { File.Delete(path); } } catch { } throw; } }
private static extern int AVIStreamInfo(int pAVIStream, ref AVISTREAMINFOW psi, int lSize);
/// <summary>Initialize the AviFile.</summary> /// <param name="path">The path to the output file.</param> /// <param name="frameRate">The frame rate for the video.</param> /// <param name="width">The width of the video.</param> /// <param name="height">The height of the video.</param> /// <param name="quality">Video quality 0 to 10000.</param> public AviWriter(string path, int frameRate, int width, int height, uint quality = 10000) { #region Validation if (path == null) { throw new ArgumentNullException(nameof(path)); } if (frameRate <= 0) { throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "The frame rate must be at least 1 frame per second."); } if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), width, "The width must be at least 1."); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), height, "The height must be at least 1."); } #endregion //Store parameters. _width = width; _height = height; _disposed = false; //Get the stride information by creating a new bitmap and querying it using (var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); _stride = (uint)bmpData.Stride; bmp.UnlockBits(bmpData); } try { //Initialize the AVI library. AVIFileInit(); //Open the output AVI file. var rv = AVIFileOpen(out _aviFile, path, AVI_OPEN_MODE_CREATEWRITE, IntPtr.Zero); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } //Create a new stream in the avi file. var aviStreamInfo = new AVISTREAMINFOW { fccType = GetFourCc("vids"), fccHandler = GetFourCc("CVID"), //CVID// 808810089, //IV50 //'DIB '//MJPG dwScale = 1, dwRate = (uint)frameRate, dwSuggestedBufferSize = (uint)(_height * _stride), dwQuality = quality, //-1 default 0xffffffff, 0 to 10.000 rcFrame = new Native.Rect { Bottom = _height, Right = _width } }; rv = AVIFileCreateStream(_aviFile, out _aviStream, ref aviStreamInfo); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } //Set compress options. var options = new AVICOMPRESSOPTIONS { fccType = GetFourCc("vids"), lpParms = IntPtr.Zero, lpFormat = IntPtr.Zero }; var mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AVICOMPRESSOPTIONS))); Marshal.StructureToPtr(options, mem, false); var streams = new[] { _aviStream }; var infPtrs = new[] { mem }; var ok = AVISaveOptions(IntPtr.Zero, ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE, 1, streams, infPtrs); if (ok) { options = (AVICOMPRESSOPTIONS)Marshal.PtrToStructure(mem, typeof(AVICOMPRESSOPTIONS)); } Marshal.FreeHGlobal(mem); if (!ok) { throw new Exception("User cancelled the operation."); } rv = AVIMakeCompressedStream(out _compStream, _aviStream, ref options, 0); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } //Configure the compressed stream. var streamFormat = new BITMAPINFOHEADER { biSize = 40, biWidth = _width, biHeight = _height, biPlanes = 1, biBitCount = 24, biSizeImage = (uint)(_stride * _height), biCompression = 0 //BI_RGB }; rv = AVIStreamSetFormat(_compStream, 0, ref streamFormat, 40); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } } catch { //Clean up. Dispose(false); try { if (File.Exists(path)) { File.Delete(path); } } catch { } throw; } }
/// <summary>Initialize the AviFile.</summary> /// <param name="path">The path to the output file.</param> /// <param name="frameRate">The frame rate for the video.</param> /// <param name="width">The width of the video.</param> /// <param name="height">The height of the video.</param> /// <param name="fourcc">The FOURCC compression value to use. A value of null means no compression is used.</param> public AviImageWriter(string path, int frameRate, int width, int height) { // Validate parameters if (path == null) { throw new ArgumentNullException("path"); } if (frameRate <= 0) { throw new ArgumentOutOfRangeException("frameRate", frameRate, "The frame rate must be at least 1 frame per second."); } if (width <= 0) { throw new ArgumentOutOfRangeException("width", width, "The width must be at least 1."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", height, "The height must be at least 1."); } // Store parameters uint fccType = GetFourCc("vids"); _width = width; _height = height; _disposed = false; // Get the stride information by creating a new bitmap and querying it using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); _stride = (uint)bmpData.Stride; bmp.UnlockBits(bmpData); } try { // Initialize the AVI library AVIFileInit(); // Open the output AVI file int rv = AVIFileOpenW(ref _aviFile, path, AVI_OPEN_MODE_CREATEWRITE, 0); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } // Create a new stream in the avi file AVISTREAMINFOW aviStreamInfo = new AVISTREAMINFOW(); aviStreamInfo.fccType = fccType; aviStreamInfo.fccHandler = 0; aviStreamInfo.dwScale = 1; aviStreamInfo.dwRate = (uint)frameRate; aviStreamInfo.dwSuggestedBufferSize = (uint)(_height * _stride); aviStreamInfo.dwQuality = 0xffffffff; aviStreamInfo.rcFrame = new RECT(); aviStreamInfo.rcFrame.bottom = _height; aviStreamInfo.rcFrame.right = _width; rv = AVIFileCreateStream(_aviFile, out _aviStream, ref aviStreamInfo); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); } // Configure the compressed stream BITMAPINFOHEADER streamFormat = new BITMAPINFOHEADER(); streamFormat.biSize = 40; streamFormat.biWidth = _width; streamFormat.biHeight = _height; streamFormat.biPlanes = 1; streamFormat.biBitCount = 24; streamFormat.biSizeImage = (uint)(_stride * _height); rv = AVIStreamSetFormat(_aviStream, 0, ref streamFormat, 40); if (rv != 0) { throw new Win32Exception(((AviErrors)rv).ToString()); //, "Unable to set the AVI stream format."); } } catch { // Clean up Dispose(false); try { if (path != null && File.Exists(path)) { File.Delete(path); } } catch {} throw; } }
/// <summary>Initialize the AviFile.</summary> /// <param name="path">The path to the output file.</param> /// <param name="frameRate">The frame rate for the video.</param> /// <param name="width">The width of the video.</param> /// <param name="height">The height of the video.</param> /// <param name="fourcc">The FOURCC compression value to use. A value of null means no compression is used.</param> public AviImageWriter(string path, int frameRate, int width, int height) { // Validate parameters if (path == null) throw new ArgumentNullException("path"); if (frameRate <= 0) throw new ArgumentOutOfRangeException("frameRate", frameRate, "The frame rate must be at least 1 frame per second."); if (width <= 0) throw new ArgumentOutOfRangeException("width", width, "The width must be at least 1."); if (height <= 0) throw new ArgumentOutOfRangeException("height", height, "The height must be at least 1."); // Store parameters uint fccType = GetFourCc("vids"); _width = width; _height = height; _disposed = false; // Get the stride information by creating a new bitmap and querying it using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); _stride = (uint)bmpData.Stride; bmp.UnlockBits(bmpData); } try { // Initialize the AVI library AVIFileInit(); // Open the output AVI file int rv = AVIFileOpenW(ref _aviFile, path, AVI_OPEN_MODE_CREATEWRITE, 0); if (rv != 0) throw new Win32Exception(((AviErrors)rv).ToString()); // Create a new stream in the avi file AVISTREAMINFOW aviStreamInfo = new AVISTREAMINFOW(); aviStreamInfo.fccType = fccType; aviStreamInfo.fccHandler = 0; aviStreamInfo.dwScale = 1; aviStreamInfo.dwRate = (uint)frameRate; aviStreamInfo.dwSuggestedBufferSize = (uint)(_height * _stride); aviStreamInfo.dwQuality = 0xffffffff; aviStreamInfo.rcFrame = new RECT(); aviStreamInfo.rcFrame.bottom = _height; aviStreamInfo.rcFrame.right = _width; rv = AVIFileCreateStream(_aviFile, out _aviStream, ref aviStreamInfo); if(rv != 0) throw new Win32Exception(((AviErrors)rv).ToString()); // Configure the compressed stream BITMAPINFOHEADER streamFormat = new BITMAPINFOHEADER(); streamFormat.biSize = 40; streamFormat.biWidth = _width; streamFormat.biHeight = _height; streamFormat.biPlanes = 1; streamFormat.biBitCount = 24; streamFormat.biSizeImage = (uint)(_stride * _height); rv = AVIStreamSetFormat(_aviStream, 0, ref streamFormat, 40); if (rv != 0) throw new Win32Exception(((AviErrors)rv).ToString()); //, "Unable to set the AVI stream format."); } catch { // Clean up Dispose(false); try { if (path != null && File.Exists(path)) File.Delete(path); } catch{} throw; } }