public static void GetBitmapInfo(byte[] data, out int width, out int height, out int bpp) { var infoHeader = new BitmapInfoHeader(); int pos = 0; int rawsize = Marshal.SizeOf(infoHeader.GetType()); IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.Copy(data, pos, buffer, rawsize); infoHeader = (BitmapInfoHeader)Marshal.PtrToStructure(buffer, infoHeader.GetType()); Marshal.FreeHGlobal(buffer); width = infoHeader.biWidth; height = infoHeader.biHeight; bpp = infoHeader.biBitCount; }
/// <summary> /// Create new AVI file and open it for writing. /// </summary> /// /// <param name="fileName">AVI file name to create.</param> /// <param name="width">Video width.</param> /// <param name="height">Video height.</param> /// /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks> /// /// <exception cref="ApplicationException">Failure of opening video files (the exception message /// specifies the issues).</exception> /// public bool Open(string fileName, int width, int height) { lock (this) { // calculate stride stride = width * 4; if ((stride % 4) != 0) { stride += (4 - stride % 4); } this.width = width; this.height = height; // describe new stream Avi32.AVISTREAMINFO info = new Avi32.AVISTREAMINFO(); LOG.InfoFormat("Available codecs: {0}", String.Join(", ", Avi32.AvailableCodecs.ToArray())); info.type = Avi32.mmioFOURCC("vids"); if (codec != null) { info.handler = Avi32.mmioFOURCC(codec); } else { info.handler = Avi32.mmioFOURCC("DIB "); } info.scale = 1; info.rate = rate; info.suggestedBufferSize = stride * height; try { // create new file if (Avi32.AVIFileOpen(out file, fileName, Avi32.OpenFileMode.Create | Avi32.OpenFileMode.Write, IntPtr.Zero) != 0) { throw new ApplicationException("Failed opening file"); } // create stream if (Avi32.AVIFileCreateStream(file, out stream, ref info) != 0) { throw new ApplicationException("Failed creating stream"); } // describe compression options Avi32.AVICOMPRESSOPTIONS options = new Avi32.AVICOMPRESSOPTIONS(); // uncomment if video settings dialog is required to show int retCode = 0; if (codec == null) { retCode = Avi32.AVISaveOptions(stream, ref options); if (retCode == 0) { LOG.Debug("Cancel clicked!"); return(false); } codec = Avi32.decode_mmioFOURCC(options.handler); quality = options.quality; } else { options.handler = Avi32.mmioFOURCC(codec); options.quality = quality; } LOG.DebugFormat("Codec {0} selected with quality {1}.", codec, quality); AviError retval; // create compressed stream try { retval = Avi32.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero); } catch (Exception exCompress) { LOG.Warn("Couldn't use compressed stream.", exCompress); retval = AviError.AVIERR_OK; } if (retval != AviError.AVIERR_OK) { throw new ApplicationException(string.Format("Failed creating compressed stream: {0}", retval)); } // describe frame format BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(width, height, 32); // set frame format if (streamCompressed != IntPtr.Zero) { retval = Avi32.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())); } else { retval = Avi32.AVIStreamSetFormat(stream, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())); } if (retval != 0) { throw new ApplicationException(string.Format("Failed creating stream: {0}", retval)); } position = 0; return(true); } catch (Exception ex) { Close(); Avi32.AVIFileExit(); if (File.Exists(fileName)) { File.Delete(fileName); } throw ex; } } }
/// <summary> /// Create new AVI file and open it for writing. /// </summary> /// /// <param name="fileName">AVI file name to create.</param> /// <param name="width">Video width.</param> /// <param name="height">Video height.</param> /// /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks> /// /// <exception cref="ApplicationException">Failure of opening video files (the exception message /// specifies the issues).</exception> /// public void Open(string fileName, int width, int height) { lock (this) { // calculate stride stride = width * 4; if ((stride % 4) != 0) { stride += (4 - stride % 4); } // create new file if (Avi32.AVIFileOpen(out file, fileName, Avi32.OpenFileMode.Create | Avi32.OpenFileMode.Write, IntPtr.Zero) != 0) { throw new ApplicationException("Failed opening file"); } this.width = width; this.height = height; // describe new stream Avi32.AVISTREAMINFO info = new Avi32.AVISTREAMINFO(); info.type = Avi32.mmioFOURCC("vids"); if (codec != null) { info.handler = Avi32.mmioFOURCC(codec); } else { info.handler = Avi32.mmioFOURCC("DIB "); } info.scale = 1; info.rate = rate; info.suggestedBufferSize = stride * height; // create stream if (Avi32.AVIFileCreateStream(file, out stream, ref info) != 0) { throw new ApplicationException("Failed creating stream"); } // describe compression options Avi32.AVICOMPRESSOPTIONS options = new Avi32.AVICOMPRESSOPTIONS(); // uncomment if video settings dialog is required to show if (codec == null) { Avi32.AVISaveOptions(stream, ref options); } else { options.handler = Avi32.mmioFOURCC(codec); options.quality = quality; } // create compressed stream int retval = Avi32.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero); if (retval != 0) { throw new ApplicationException("Failed creating compressed stream: " + retval); } // describe frame format BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(width, height, 32); // set frame format retval = Avi32.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())); if (retval != 0) { throw new ApplicationException("Failed creating compressed stream: " + retval); } position = 0; } }