示例#1
0
文件: Vfw.cs 项目: tdenc/nicorank
        public void AddWave(string wave_filename)
        {
            byte[]       wave_data = File.ReadAllBytes(wave_filename);
            MemoryStream header    = new MemoryStream();

            header.Write(wave_data, 0, 36);
            header.Seek(0, SeekOrigin.Begin);
            BinaryReader reader = new BinaryReader(header);

            reader.ReadBytes(20);

            IntPtr wave_ptr      = Marshal.AllocHGlobal(wave_data.Length);
            IntPtr wave_data_ptr = new IntPtr(wave_ptr.ToInt32() + 44);

            try
            {
                Marshal.Copy(wave_data, 0, wave_ptr, wave_data.Length);

                VfwApi.PCMWAVEFORMAT pwformat = new VfwApi.PCMWAVEFORMAT();
                pwformat.wFormatTag      = reader.ReadInt16();
                pwformat.nChannels       = reader.ReadInt16();
                pwformat.nSamplesPerSec  = reader.ReadInt32();
                pwformat.nAvgBytesPerSec = reader.ReadInt32();
                pwformat.nBlockAlign     = reader.ReadInt16();
                pwformat.wBitsPerSample  = reader.ReadInt16();

                int sample_size = pwformat.nChannels * pwformat.wBitsPerSample / 8;

                CreateAudioStream(pwformat.nSamplesPerSec, (wave_data.Length - 44) / sample_size, sample_size);

                int hr = VfwApi.AVIStreamSetFormat(pavi_audio_, 0, ref pwformat, Marshal.SizeOf(pwformat));
                if (hr != 0)
                {
                    throw new VfwException("AVIStreamSetFormat", hr);
                }

                hr = VfwApi.AVIStreamWrite(pavi_audio_, 0, wave_data.Length - 44, wave_data_ptr,
                                           wave_data.Length - 44, 16, IntPtr.Zero, IntPtr.Zero);
                if (hr != 0)
                {
                    throw new VfwException("AVIStreamWrite", hr);
                }

                VfwApi.AVIStreamRelease(pavi_audio_);
                pavi_audio_ = IntPtr.Zero;
            }
            finally
            {
                Marshal.FreeHGlobal(wave_ptr);
            }
        }
示例#2
0
文件: Vfw.cs 项目: tdenc/nicorank
        // 引数に与えた bitmap は上下反転される
        public void AddFrame(Bitmap bitmap)
        {
            if (is_first)
            {
                int bit_count = PixelFormatToBitCount(bitmap.PixelFormat);
                width_        = bitmap.Width;
                height_       = bitmap.Height;
                pixel_format_ = bitmap.PixelFormat;
                stride_       = (width_ * bit_count / 8 + 3) & ~3;
                CreateVideoStream(width_, height_, stride_, rate_, scale_);
                SetVideoFormat(width_, height_, stride_, bit_count);
                is_first = false;
            }
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            BitmapData bmp_data = bitmap.LockBits(new Rectangle(0, 0, width_, height_), ImageLockMode.ReadOnly, pixel_format_);
            int        hr       = VfwApi.AVIStreamWrite(pavi_, current_frame_, 1, bmp_data.Scan0, stride_ * height_, 16, IntPtr.Zero, IntPtr.Zero);

            if (hr != 0)
            {
                throw new VfwException("AVIStreamWrite", hr);
            }
            bitmap.UnlockBits(bmp_data);
            ++current_frame_;
        }